Sunday, January 30, 2011

The difference between unit tests and integration tests

Developers usually write two types of tests for their code, unit tests and integration tests.

Unit tests are written for testing the correctness of the smallest unit of code in an application. In object oriented programming such a unit is usually a method. The unit must be tested in isolation when unit testing. This means that any dependency the unit has, such as a database, service or file system, must be abstracted away and replaced by a fake object. A fake object can be created manually or by using a mocking framework.

Integration tests on the other hand are for testing the correctness of the unit under test together with all of its dependencies. In such a test it's allowed to call a database, service, file system or any other type of dependency the code might have.