Practice: catch a real bug

Find three real bugs with tests, then fix them — in that order.

16of 16
≈ 15Min read
3Parts

The order matters

Write the failing test first, for each bug. You get the reproduction, the definition of done and the regression test in one move — and you cannot "fix" something that was never broken, which happens more often than anybody admits.

Then fix them one at a time, watching the right test go green each time. A fix that turns three tests green at once usually means the tests were not independent.

How to find them without reading the code first

  • The boundaries. Zero, one, the maximum, the empty list, the missing field.
  • The arithmetic. Rounding, percentages, and anything involving money.
  • The order of operations. Does the discount apply before or after the tax? Both are defensible; only one is what the business meant.
  • The invariants. The parts must add up to the whole. A total must never be negative. A copy must not change the original.

And when a test disagrees with the code

Decide which is right before changing either. The test might be asserting a bug that shipped last year and that a customer now depends on. That is a conversation, not a code change — and the test is the artefact that starts it.

Practice