Do I need to test my code?
Some people would say, "Why should I test?" or ,"I don't need to write a test case. My code works the way I expect it to function." Well, I assure you that other people, end users, would expect your code to behave the way you want, too, but there are cases that your working program will crash and cause errors and will be a major embarrassment.
On the other hand, some other programmers would test their code at the end of the development phase, as it is the case in the traditional waterfall software development life cycle as taught in the software engineering course that uses a dated curriculum. This is usually going to be a disaster because you built a system and assumed that it would work fine, but there was no testing involved while developing it.
Therefore, in order to avoid such embarrassments and program crashes, do test your code on regular basis. First thing is to make sure that you build modular functions that execute the way they are intended to, and of course, the way the are named. For example, if you build a function called "add()", it should only do the addition, and nothing else.
After spending sometime on building your modular and meaningful function, test it. Do this step after each function you code and check if it is working correctly. This is called unit testing. For instance, you have a function that divides numbers called "divide()" and let us assume that you are using a command line version of this program, you divide some numbers and it gives you the proper results. A corner case is when you divide a number by zero, which will give an exception error and will crash your program. So, you write a try-catch block to prevent such an error to happen in the future. The way that a beginner programmer will test is by running the code and looking for results. Honestly, that will work if it is an assignment or something that you are building for yourself. That method will not work if building a big system or working for a company.
A proper way to perform a unit test is by writing a test case. This way, you will write any corner cases that you think can crash the code, you run the test, the test fails, you fix your code, you run the test again, it fails, you fix it again, you run it again, and then the test passes. By this method, you are able to fix any problems in your code earlier, and you can refactor your code as well as moving forward. In other words, you design your code and document it. Design and documentation are important software engineering principles, right? Yes and you are fulfilling a customer's requirements and matching your unit test documentation with the actual documentation if exists.
From experience and practice, the beginner way of testing will only work for small projects and assignments, but if the same method applied for a big website for example, it will be time consuming and mundane for you as a developer as you try to login and then perform some manual tests. Unit testing will cut that time and make your life easy. You will be writing a test case for a use case. An example of a use case, a user logs in the website, if he/she is an admin, the admin page will be viewed, otherwise a regular user's page will be shown. So in your unit tests, you will be following such an example and test your function if that webpage will view an admin page or a regular one. Then you might think what if a user was not in the database, and so forth. Then you start covering all the possible problems and corner cases.
There are many test frameworks available such as JUnit for Java, NUnit for C#, and many others. Some of them are built in the language like Python, in which you import the unit test library.
In modern software engineering principles, unit testing is a skill that every programmer should know; A programmer is also a tester. It is one of the Agile methodologies requirements, in which testing your code before shipping it is a must, otherwise, you don't ship it. And as is being inspired from unit testing, other extensions were introduced and are now commonly used such as TDD (Test Driven Development), and recently BDD (Behavior Driven Development) which I will leave the liberty for the reader to continue on these topics.