Test-Driven Bug Fixes
Bug fixes need tests too. The existence of a bug often
shows where prior test efforts have failed. If we can
write a unit test to reveal the bug, do so. If
investigation is needed to track down the bug, do the
investigation and capture some of your knowledge in
the tests as you go. Once the bug has been located,
write a unit test that reveals the bug. Resist the
temptation to immediately fix the bug.
You want to make sure you don’t give life to new
bugs when you kill the bug you were hunting. Also,
bugs have been known to nest together. Both of these
realities mean you should also write tests to
lock in the desired behavior before fixing a bug.
As you noticed in crash to pass, the
first test in a new area usually has high startup
costs. Subsequent tests usually can be written pretty
quickly. This will be true when you start adding tests
during the bug hunt. Think of the cost of adding
a test for the bug as the cost of doing business—as
the price to pay down the technical debt. Defects
are costly. Fix them right; it’s only a fractional
cost.