What is Unit Testing?

Unit Testing is writing tests that have these properties:

Unit testing should be done 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 testing

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

The unit testing 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 testing 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 testing 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