What is Unit Testing?
Unit Testing is also known as a component, module, and program testing. The smallest independent and testable part of the source code is referred to as a unit. It is done mostly by the developers. Unit testing searches for defects in a module and verifies the functioning of software w.r.t modules, programs, objects, classes, etc. It is done in the isolation environment from the rest of the system.
It uses stubs and drivers to replace the missing software and with the help of the simulation of the interface between the software, components are done. A stub is called from the software component to be tested; a driver calls a component to be tested. It includes testing of functionality as well as memory leaks, performance, or robustness testing.
It is tested in a development environment with the help of a unit test framework or debugging tool. It is an informal way of testing where defects are not recorded and are typically fixed as soon as they are found. This makes a strong foundation of the software and development process is carried out at a much faster pace thereby saving a lot of time. It happens mostly in the test-driven development approach.
Types of Unit Testing
There are 2 types of Unit Testing
Unit testing is commonly automated but still, it may be performed manually. But the automation is preferred over the latter.
Objective of Unit Testing
- To verify bugs early in the development cycle and save costs.
- To isolate a section of code.
- To help developers to understand the code base and enables them to make changes quickly.
- To test every function and procedure.
- To verify the correctness of code.
- To help with code reuse.
Workflow of Unit Testing
Check out code from repository:
The first step is always used to take the latest code from the repository. This is one of the best practices that need to be followed in the software development life cycle. It helps in smooth check-in of the code thereby eliminating maximum conflicts. Also, it ensures that the new code change build is always made on the latest repository code.
Make code change:
The new implementation of code or modification of the existing code is done at this phase. The developer writes code to implement the new feature and product backlog. The changed code ensures the efficiency of execution and response time.
Execute Unit Tests written for the change:
In this phase, the unit tests are defined to test the newly implemented code. The tests are written so that it satisfies the acceptance criteria. The tests are further executed in the developer’s environment in the first run. The testing is performed in an informal way without raising defects for the failed tests.
Fix defects and Re-execute Unit Tests:
The defects found while execution of the unit tests are fixed by making appropriate code change. The unit tests are triggered once again to verify the code change made should work as expected. It also looks for the impact of the change made to fix the defect.
Once all the unit tests are passed, the developer request for the code review to their co-developers and architects. The review comments provided to the developer have to be incorporated into the code and unit tests have to be re-run to ensure the functionality works as expected.
Check-in code into the repository:
After all the above processes, the code is check-in to the repository. Any conflicts raised while check-in has to be resolved and then check-in should be done successfully.
Unit Testing Techniques
- Black Box Testing – It is used when the user interface, input, and output are to be tested.
- White Box Testing -It is used when function behavior is to be tested.
- Gray Box Testing – It is used to execute tests, risks and assessment methods
Unit Testing Tools
Here are some commonly used Unit Testing tools:
Advantages of Unit Testing
- It increases confidence in changing/ maintaining code. Unit tests are written are run every time, and whenever any code is changed it will be able to promptly catch the defects introduced due to the change.
- Codes reusability.
- Unit testing enables us to test parts of the project without waiting for others to be completed.
- The cost of fixing a defect detected during unit testing is lesser in comparison to that of defects detected at higher levels.
- Makes debugging easy.
Disadvantages of Unit Testing
- Time-consuming process
- Does not catch all the errors in the program