Chapter 10 identified a number of principles that can be used to guide us toward writing effective unit tests. This chapter builds on these principles to cover a number of practical techniques that we can apply in our everyday coding.
Chapter 10 described the key features that good unit tests should exhibit. The motivation for many of the techniques described in this chapter directly follow from these, so as a reminder, the key features are as follows:
- Accurately detects breakages—If the code is broken, a test should fail. And a test should fail only if the code is indeed broken (we don’t want false alarms).
- Agnostic to implementation details—Changes in implementation details should ideally not result in changes to tests.
- Well-explained failures—If the code is broken, the test failure should provide a clear explanation of the problem.
- Understandable test code—Other engineers need to be able to understand what exactly a test is testing and how it is doing it.
- Easy and quick to run—Engineers usually need to run unit tests quite often during their everyday work. A slow or difficult-to-run unit test will waste a lot of engineering time.