Unit Testing – Small units that pave way to a smooth software testing
In software Testing, Unit Testing is that level of testing process where individual units/components of a software/system are tested. The purpose is to validate that each unit of the software performs as designed. Unit testing is often automated but it can also be done manually. This testing mode is a component of Extreme Programming (XP), a pragmatic method of software development that takes a meticulous approach to building a product by means of continual testing and revision.
Unit Testing is important because:
- Suppose you have two units and you do not want to test the units individually but as an integrated system to save your time, unit testing helps.
- Once the system is integrated and later you find an error in an integrated system it becomes a hassle to differentiate in which unit the error has occurred, so unit testing is mandatory before integrating the units.
- When developer is coding the software it may happen that the dependent modules are not completed for testing, in such cases developers use stubs and drivers to simulate the called (stub) and caller (driver) units. Unit testing requires stubs and drivers, stubs simulate the called unit and driver simulates the calling unit.
A good unit test:
- Has the ability to be completely automated
- Has full control over all the pieces running (Use mocks or stubs to achieve this isolation when needed)
- Runs in memory and runs at a good speed
- Tests a single logical concept in the system
- Readable and maintainable
- Trustworthy (when you see its result, you don’t need to debug the code to be assured)
- Unit testing increases confidence in changing/maintaining code. If good unit tests are written and if they are run every time any code is changed, the likelihood of any defects due to the change being promptly caught is very high. If unit testing is not in place, the maximum one can do is hope for the best and wait till the test results at higher levels of testing are out. Also, if codes are already made less interdependent to make unit testing possible, the unintended impact of changes to any code is less.
- Codes are more reusable: In order to make unit testing possible, codes need to be modular. This means that codes are easier to reuse.
- Development is faster: The effort required to find and fix defects found during unit testing is equal to nothing in comparison to those found during system testing or acceptance testing. Unit tests are more reliable than developer tests.
- The cost of fixing a defect detected during unit testing is lesser in comparison to that of defects detected at higher levels. Compare the cost (time, effort) of a defect detected during acceptance testing or say when the software is live.
- Debugging is easy: When a test fails, only the latest changes need to be debugged. With testing at higher levels, changes made over the span of time need to be debugged.
The Disadvantages of unit-testing:
- From a busy development team’s point of view, the biggest disadvantage of taking on unit-testing is the initial time required to develop them. It’s been estimated that it takes approximately 30% more time at project start up to begin putting unit-tests into place. That’s a significant commitment of resources and in many teams with tight deadlines to meet – it’s a big deterrent to implement unit-testing.
- There is also a smaller time commitment required to give your developers the skills to develop unit-tests. This is true of almost any new process that you add within a business and really shouldn’t be significant enough to discourage the use of unit-tests.
In Unit Testing, each unit is tested separately before integrating them into modules to test the interfaces between modules. Unit testing has proven its value in that a large percentage of defects are identified during its use.