Unit Testing

Why unit test?

  • Less time performing functional tests
  • Protection against regression
  • Executable documentation
  • Less coupled code

Characteristics of a good unit test:

  1. Fast. It is not uncommon for mature projects to have thousands of unit tests. Unit tests should take very little time to run. Milliseconds.
  2. Isolated. Unit tests are standalone, can be run in isolation, and have no dependencies on any outside factors such as a file system or database.
  3. Repeatable. Running a unit test should be consistent with its results, that is, it always returns the same result if you do not change anything in between runs.
  4. Self-Checking. The test should be able to automatically detect if it passed or failed without any human interaction.
  5. Timely. A unit test should not take a disproportionately long time to write compared to the code being tested. If you find testing the code taking a large amount of time compared to writing the code, consider a design that is more testable.