Home

 

Benefits of unit and component testing

It might seem obvious as to why we want to test our code. Unfortunately, many people do not understand the value of testing. Simply put, we test our code and applications to find defects in the code, and to verify that changes we have made to existing code do not break that code. In this section, we highlight the key benefits of unit and component testing.

Perhaps it is more useful to look at the question from the opposite perspective, that is, why developers do not perform unit tests. In general, the simple answer is because it is too hard or because nobody forces them to. Writing an effective set of unit tests for a component is not a trivial undertaking. Given the pressure to deliver that many developers find themselves subjected to, the temptation to postpone the creation and execution of unit tests in favor of delivering code fixes or new functionality is often overwhelming.

In practice, this usually turns out to be a false economy, because developers very rarely deliver bug-free code, and the discovery of code defects and the costs associated with fixing them are simply pushed further out into the development cycle, which is inefficient. The best time to fix a code defect is immediately after the code has been written, while it is still fresh in the developer's mind.

Furthermore, a defect discovered during a formal testing cycle must be written up, prioritized, and tracked. All of these activities incur cost, and might mean that a fix is deferred indefinitely, or at least until it becomes critical.

Based on our experience, we believe that encouraging and supporting the development and regular execution of unit test cases ultimately leads to significant improvements in productivity and overall code quality. The creation of unit test cases does not have to be a burden. If done properly, developers can find the intellectual challenge quite stimulating and ultimately satisfying. The thought process involved in creating a test can also highlight shortcomings in a design, which might not otherwise have been identified when the main focus is on implementation.

We recommend that you take the time to define a unit testing strategy for your own development projects. A simple set of guidelines, and a framework that makes it easy to develop and execute tests, pays for itself surprisingly quickly.

After you have decided to implement a unit testing strategy for your project, the first hurdles to overcome are the factors that dissuade developers from creating and running unit tests in the first place. A testing framework can help by making it easier to:

Write tests

Run tests

Rerun a test after a change

Tests are easier to write, because a lot of the infrastructure code that you require to support every test is already available. A testing framework also provides a facility that makes it easier to run and re-run tests, perhaps via a GUI. The more often a developer runs tests, the sooner the problems can be located and fixed, because the difference between the code that last passed a unit test, and the code that fails the test, is smaller.

ibm.com/redbooks