It’s not just about Write tests, but writing tests that you can trust.
Test the behaviour, not the implementation.
Only test the things that could possibly break. Kent Beck
Thumb rules:
- A flaky test is worth less than no test.
- It’s hard to test the behaviour instead of the implementation; try to test a group of functions instead of testing each function individually. Test the usage, the facade, and the business rules.
- If you test what changes state, you don’t need to test what returns state, because you will be using those functions to validate that the state changed anyway.
- I like to write an excessive amount of tests, then remove them as part of my self-review to reduce test brittleness. Test is code, so “Write less code” applies for tests as well.
Part of Tips for Software Engineers