Saturday, April 19, 2008

What is a Unit Test and Why Unit Test?

What is a Unit Test?

A unit test is a test of the correctness of an individual code module, for example, a test of a method or class. Typically, developers create unit tests for their code as they develop it. A typical unit test is a method that executes the method that it tests and verifies that the method generates the correct output for a given set of inputs.
Unit tests differ from integration tests. Integration tests verify the correctness of the interaction of a set of code modules. Unit tests verify only the correctness of a code module in isolation. Integration tests of a set of code modules could fail even though each module passes its unit tests.

Why Unit Test?

Unit testing offers a number of benefits including:
Providing verification that a code module is correct. This is the principal motivation for unit testing.
Providing automatic regression testing. Unit tests should be re-run after changes to a code module to ensure that code module is still correct. That is, unit tests should be used to ensure that changes have not broken a code module. Ideally, the unit tests for all code modules should be run after a change to any one module.
Providing documentation. Frequently, the unit tests of a code module are delivered together with the code module. Examining the unit tests provides a great deal of information about how a code module functions.

No comments: