Sub topic 9
Verify the fix with a test
This chapter assumes you can run Python code and pytest from a terminal, use basic git commands, and have already reproduced a failure and received a proposed fix from an assistant. Here you will confirm that the fix works and that it did not break anything else.
Why "it looks right" is not verification
An assistant's fix can read well and still be wrong. It may fix the failing case and break another one. It may also hide the error instead of removing the cause. The only reliable check is to run the code against evidence you control.
Verification answers two questions:
- Does the original failure go away?
- Does everything that worked before still work?
Start from the reproduction
In the earlier chapters you reduced the failure to a small example. That example is your first check. It should fail before the fix and pass after it.
def average(values):
return sum(values) / len(values)
print(average([]))Running this raises ZeroDivisionError: division by zero. The expected behavior, stated in the prompt, was a clear error telling the caller the input is empty.
Apply the proposed fix
The assistant proposed the smallest change that matches the expected behavior:
def average(values):
if not values:
raise ValueError("average() requires at least one value")
return sum(values) / len(values)Before you run anything, commit or stash your working state so you can compare and undo. Then make this one change.
- return sum(values) / len(values)
+ if not values:
+ raise ValueError("average() requires at least one value")
+ return sum(values) / len(values)Turn the reproduction into a test
Running the script by hand and reading the output is fragile. A test records the expected behavior in code, so anyone can re-run it. Write one test for the bug and one for normal input.
import pytest
from stats import average
def test_average_of_numbers():
assert average([2, 4, 6]) == 4
def test_average_of_empty_list_raises():
with pytest.raises(ValueError):
average([])The first test guards existing behavior. The second encodes the fix. Together they cover both verification questions.
Watch the test fail first
A test that has never failed has not proved anything. Confirm that the bug test fails without the fix:
- Stash or revert the fix, leaving the test file in place.
- Run
pytest. The empty-list test should fail, becauseZeroDivisionErroris raised instead ofValueError. - Restore the fix and run
pytestagain. Both tests should pass.
If the bug test passes without the fix, the test is not checking the bug. Rewrite it before trusting it.
Run more than the new test
Run the whole suite, not only the new tests. Assistants sometimes change shared code, and a fix in one place can break a caller somewhere else. For example, other code may have relied on average([]) raising ZeroDivisionError and catches that exact type. Your full suite, or a search for callers, will reveal it.
Common verification mistakes
| Mistake | Why it misleads | Better practice |
|---|---|---|
| Testing only the case from the bug report | Misses regressions in normal input | Add a test for ordinary input |
| Letting the assistant edit the test to make it pass | The test may now accept wrong behavior | Review any test changes line by line |
| Skipping the "fails first" check | The test may never have detected the bug | Run it against the unfixed code |
| Running only one test file | Hides breakage elsewhere | Run the full suite |
Check the diff
Finish with git diff. The change should be as small as you expected. Unrelated edits, renamed variables, or reformatted files are signs the assistant did more than you asked. Revert those parts and keep only the fix.
Exercise
You apply an assistant's fix and the new test passes. What two further checks do you need before you trust the fix?
Show answer
Confirm the test fails when the fix is removed, so you know it detects the bug. Then run the full test suite to check that nothing else broke. Reviewing the git diff for unrelated changes is also worthwhile.
Exercise
An assistant suggests a fix and also changes pytest.raises(ValueError) to pytest.raises(Exception) in your test. Why is this a problem?
Show answer
Exception matches almost any error, including the original ZeroDivisionError. The test would pass even without the fix, so it no longer verifies the bug is fixed.