Unit testing is a type of software testing where the individual units or components of software are tested. The goal is to verify that each unit of the software code performs as expected. Unit testing is done during the development of an application by the developers. Unit tests isolate a section of code and verify its accuracy.
Unit testing is important because software developers try to save time doing minimal unit testing. Inappropriate unit testing may lead to higher defect fixing cost. If the defects are identified at later test levels like integration testing, system testing, and even beta testing after the application is built the defect fixing cost will be higher.
The main reasons to perform unit testing are:
- Unit testing helps us to fix the bugs early in the development phase and save costs.
- It helps developers to understand the testing code base and also activates them to make changes quickly.
- Good unit tests give project documentation.
- Unit test help to re-use the code.
- Migrate your code and test to our new project.
How to do unit testing?
To do unit testing developers write the section of code to test a specific function in software application. Developers can also isolate this function to test rigorously so that unnecessary dependencies between functions being tested and other units so the dependencies can be eliminated.
Unit testing can be done in two ways:
Unit testing is basically automated but may be still performed manually. Software engineering doesn’t favour over the other but automation is preferred.
When automated approach is done?
A developer writes the section code in the application, just to test the function. They would later comment out and finally remove the test code when the application is deployed. Developer may isolate the function to test it more rigorously. This is more detailed unit testing practice which involves copy and paste of code to its own testing environment.
A coder generally uses the unit testing framework to develop automated tests. Using this automation framework, the developer codes criteria into test to validate the accuracy of the code. The workflow of unit test includes to create test cases, review/rework, baseline and execution of test cases.
Let us understand by simple example, what is unit testing. Consider a code, for dividing two numbers. We have a simple function and a method. One main function which performs division operation which accepts two input parameters a and b and its returns the value that you get after dividing a by b and next comes the testing function as the this function is written in a ruby(for e.g.) language but execution or purpose of the function matters the most. We have two variables a and b. We are assigning values a = 6, b = 2. In the assert equal function, we are calling the divider function and also have the the first parameter which is the value expected that the division function should give. Expected output is 3 and passing the function and parameters to that function after dividing a/b which is 6/2 is 3. Assert equal function tells me that output is 3. 3 is both the expected and actual outcome. Since expected and actual outcome are same, system is working properly. This is a positive case. We can also do it by taking the negative case as we can change the expected value to 4 and see if the assert equal function returns the same answer or not. That is how testing is performed on a small unit which can be called as unit testing. It is also called as component testing.