3 Testing techniques
This chapter covers:
- Organising your tests comprehensively
- Writing assertions which are flexible and robust
- Isolating and instrumenting parts of your code for tests
- Strategies for choosing what to test and what not to
Well written tests have two main qualities: they only break when the application misbehaves, and they precisely tell you what's wrong. In this chapter, we will focus on techniques which help you achieve these two goals.
If you've written a test for the addToCart
function, for example, you don't want it to break if that function is still working. If the test does break, it will generate extra costs because you will have to spend time updating it. Ideally, your tests should be sensitive enough to catch as many bugs as possible, but sufficiently robust so that they fail only when necessary.
Considering that your tests for the addToCart
function broke for a good reason, they still wouldn't be particularly helpful if their feedback was undecipherable or if ten other unrelated tests failed when they shouldn't. A carefully architected test suite provides you with high-quality feedback to fix problems as quickly as possible.
In this chapter, to achieve high-quality feedback and robust yet sensitive tests, I will focus on how to organise tests, write assertions, isolate code, and choose what to test and how to test it.