Some characteristics of Good Tests™.
- Useful: Tests should be able to prove that the code is working as expected and should be able to detect software problems. Easier said than done, but it’s a point worth making. If your tests aren’t useful, they will get in the way of writing new features and refactoring. Resentment will creep in.
- Uniform: Tests should have a compact, uniform appearance and structure. Why compact - it makes for easier reading. Why uniform? It makes for a common testing syntax, which makes refactoring simpler and follows onto my next point.
- Understandable: The intent of the test should be clear from a glance, including its name. The test should express this as succinctly as possible. Part of this also includes using a sensible level of abstraction to conceal the mechanics of the testing, freeing you to focus on the intent.
- Predictable: Don’t try and be too clever. I’d rather do some manual work than be surprised by an unexpected side-effect.
- Isolated: The running of one test should not affect another. Test code should be as decoupled as possible. The possibility of shared mutable test state should be minimised.
- Changeble: It should be easier to update the test than the class under test. If small changes in your class require large changes to your unit test, then you’re probably on the wrong track.
Thanks for reading!
This post is Creative Commons Attribution 4.0 International (CC BY 4.0) licensed.