Chapter 1. The goal of unit testing


This chapter covers

  • The state of unit testing
  • The goal of unit testing
  • Consequences of having a bad test suite
  • Using coverage metrics to measure test suite quality
  • Attributes of a successful test suite

Learning unit testing doesn’t stop at mastering the technical bits of it, such as your favorite test framework, mocking library, and so on. There’s much more to unit testing than the act of writing tests. You always have to strive to achieve the best return on the time you invest in unit testing, minimizing the effort you put into tests and maximizing the benefits they provide. Achieving both things isn’t an easy task.

It’s fascinating to watch projects that have achieved this balance: they grow effortlessly, don’t require much maintenance, and can quickly adapt to their customers’ ever-changing needs. It’s equally frustrating to see projects that failed to do so. Despite all the effort and an impressive number of unit tests, such projects drag on slowly, with lots of bugs and upkeep costs.

That’s the difference between various unit testing techniques. Some yield great outcomes and help maintain software quality. Others don’t: they result in tests that don’t contribute much, break often, and require a lot of maintenance in general.

1.1. The current state of unit testing

1.2. The goal of unit testing

1.3. Using coverage metrics to measure test suite quality

1.4. What makes a successful test suite?

1.5. What you will learn in this book