10 Unit Test Properties
Hello! Let’s keep exploring the topic of unit tests today by exploring unit test properties.
The ten most important properties that a unit test should exhibit, in no particular order:
Focused: Narrow in scope, validating the correctness of individual pieces of code1.
Understandable: Simple enough for an engineer to quickly grasp its purpose and be confident in its correctness at a glance.
Maintainable: As code evolves, it should be easy to update.
Informative: Provides clear and detailed failure messages to help diagnose what went wrong and why.
Fast: Executes quickly to provide continuous feedback during code development.
Deterministic: Consistently passes or fails in the same way, avoiding flaky behavior.
Resilient: It shouldn’t break when we make unrelated changes to the code (opposite: brittle).
Isolated: Should be isolated from other unit tests so that the outcome of one doesn’t affect another.
Local: Shouldn’t interact with the external world (which often helps to achieve property #5).
Without sleep: There shouldn’t be any sleep statements in the testing code, as they can introduce delays and flakiness.
Would you have added any other properties? Let me know in the comments.
Tomorrow, we will explore the concept of test coverage with line and branch coverage.
However, this doesn't imply that each function should have its own dedicated unit test. We will explore this idea in a future issue with the concept of behavior-focused unit tests.