Skip to content

Characteristics of good tests

Unit tests, as well as software development, should be created according to best practices. Below you can find the most important characteristics of good tests, that we should follow.

FIRST rule

  • Fast - they should be executed fast, in order to not extend the build time and not waint long for test results

  • Isolated/Independent - tests should not depend on each other (be isolated). Single test execution should not impact on other tests in the test suite (a single test package, e.g. tests within a single test class).

  • Repeatable - should be repeatable on any environment. This means, that they should not have dependencies with other systems, e.g. the database. The configuration of other systems should not impact on the recurrence of test execution on different environments. If a test will be executed 10 times, then we should get the same result 10 times. The use of Random class, depending on the order of elements in unordered collections or using real timestamps (by using e.g. LocalDateTime.now()) can cause problems with the consistency of test results.

  • Self-checking - stating if the whole test passed (no manual interpretation).

  • Thorough — written together with the production code. We should check the test cases by providing values giving positive results, negative results and testing edge cases. Thanks to that, the tests will check our code more precisely.

Other good practices

  • Single responsibility - one test checks one functionality.
  • Second Class Citizens - test codeis is not a second category code. It should be maintained as well as production code.