You are fixing a production bug. What is the recommended first step in code?
Options
- Fix it directly in the code and deploy it
- Write a failing test that reproduces the bug (Correct option)
- Add more logging only and wait for it to happen
- Rewrite the whole module that has the bug
Answer
Write a test that reproduces the bug and fails, then fix it so the test passes
Why
A failing test proves you understood and reproduced the bug. When it turns green, you know the fix works.
The test then stays in the suite as a regression test: if anyone reintroduces the bug later, it fails immediately.
Remember
Bug fix = failing test first, then the fix.