01
Unit Testing in Practice
The three parts of a unit test, the signal strength of an assertion, test names that describe intent, breaking order dependence, injecting dependencies from outside, the kinds of test double, what statement and branch coverage mean, and the cost of tests bound to an implementation.
- 01 The Anatomy of a Unit Test Converting a defect report's reproduction steps into a runnable test, the arrange-act-assert parts of a test body, and the expected value coming from a source independent of the implementation.
- 02 Assertions The distinction between equality and deep equality, the signal strength of an assertion measured by the number of corruptions it catches, partial matching cutting an unnecessary bond, and narrowing an error expectation with a matcher.
- 03 Test Naming Building a test name from its unit, condition, and expected-result parts, how bundling three rules into one test makes the name misleading, and how the names of passing tests form a readable list of rules.
- 04 Fast and Independent Tests Demonstrating the order dependence produced by module-level shared state with a fixed-seed permutation, breaking it with a fresh fixture, and measuring the share an out-of-process fixture adds to run time.
- 05 Testable Design Comparing a version that creates its own dependency with a version that receives it from outside, measuring fixture cost in lines, and handing off time and randomness from outside.
- 06 Test Doubles Hand-writing the dummy, stub, fake, spy, and mock types, separating the verification question each type answers, and measuring which type speaks up at the same change.
- 07 Coverage Measurement Hand-counting statement, branch, and path coverage on the same function, showing with a run the bug a codebase with one hundred percent statement coverage missed, and the limit of using a coverage percentage as a target.
- 08 Flaky Tests Implementation-bound tests breaking under a refactor with no behavior change, behavior-bound tests staying green under the same refactor, and flakiness measured with the flake rate.