Wednesday, 3 October 2007

Unit testing

The later in the lifetime of program we have to correct an error, the more expensive this is. So consequently the smaller the feedback period between coding something and knowing when something is wrong, the less expensive it is to correct the mistake.

On the other hand, you could be faced with as change request to an existing, in production working application. The programmer needs to make some changes but you would like to assure that no inconsistencies creep in the existing code parts.

On the other side of the spectrum , there is a school of thought that considers test as a specification, a way to capture what a piece of software should be doing

Unit testing is a process of testing the individual subprograms (classes), subroutines or procedures (methods) in a program. It is one of the techniques to reduce the feedback period between writing code and looking for defects in the code and is done by the programmer. This test batches could also be used for regression testing. Currently a new wave of agreement on the importance of unit testing is acknowledged in the industry. Although unit testing isn’t something completely new, new automated tools that assist the developer is his/her unit testing effort have boosted the attention to this type of testing.

Unit testing adoption requires full management support because it requires that the individual programmers adapt to a new way of working that requires a slight mind shift. Unit testing is only one kind of test. These tests don’t make other types of testing like system testing, load testing or user acceptance testing redundant.

As stated before, unit test is not something new, but the way unit testing is now supported or integrated in development tools is. For example a very popular open source: Nunit ( is freely available and enables the individual programmer to write and conduct his unit test in a very quick and uniform way sported by a tool. Microsoft also has an integrated Unit testing framework into their VS2005 Team Edition for Software Developers product.

Unit tests are written to ensure that code performs as the programmer expects—how it will be consumed, used, disposed of, and how it reacts positively, negatively, and inconclusively under any circumstance.

Unit testing requires extra work but give plenty advantages
· Basis for regression testing when changes are made
· Indirect documentation and examples the usage of the API
· …

Sometimes it is difficult to test a single unit in isolation because of its dependencies. Thought should be placed on designing code for easier testability for example via Interface based programming and Inversion Of Control. Or you should consider a tool like TypeMock (

Although the automated unit testing tools originated from the XP School you do not have to exercise “write test first, code afterwards” (Test-driven development). You can write test afterwards or better concurrently with your code.

References and recommended reading

No comments: