What are Unit Tests?

Unit Tests are tests that have these properties:

Unit tests should be written by software developers, because bugs found by them are much cheaper to solve than bugs found by a separate testing department, and even more cheaper than bugs found by customers.

Other programmers should be able to run the unit tests and add tests as well, without having to know how to run the tests. Therefore unit tests should be able to run automatically, and should be made in an easy framework that all programmers use.

How it works

unit tests

Unit tests are linked with the domain code (the code that will be in the final application) and the unit test framework. The unit test framework calls the tests, which in turn manipulate the domain code and perform checks on it.

The unit test framework will then report which tests failed. It also catches every unhandled exception, which is reported as well, so other tests can keep running.

Best practices

To use unit tests well, developers

Testing small units is important because this way you know that bugs found must be in that unit. However, integration tests can also be done with a unit test framework.

Unit tests should be isolated, so what happens in one test doesn't interfere with other tests.

It is also important to run the unit tests every time code is added. This makes sure that code changes don't cause unexpected bugs. Therefore the unit tests should run quickly. This is easier than some programmers think: the logic in code usually works just the same for small sets of data as with big sets of data.

Advantages