Sub topic 9
Using tests as evidence
This chapter assumes you can write and run unit tests with pytest, read a Git diff, and have already compared the change to its requirement. A diff can look right and still be wrong. A passing test run is evidence, but only if the tests are able to fail.
What a test can prove
A test is evidence when three things hold:
- It exercises the behavior the requirement describes.
- It would fail if that behavior were broken.
- You have seen it run.
AI assistants often write tests that check what the code does instead of what it should do. They also tend to cover the happy path and skip edge cases. A green run from tests the AI wrote alongside its own code shows consistency, not correctness.
Write the test from the requirement
Take the average function from the previous chapter.
def average(values):
return sum(values) / len(values)The requirement never said what happens for an empty list. Before you accept this function, decide what should happen and write a test that states it. Here the decision is to raise ValueError.
import pytest
def average(values):
if not values:
raise ValueError("values must not be empty")
return sum(values) / len(values)
def test_average_of_numbers():
assert average([2, 4, 6]) == 4
def test_average_of_empty_list_raises():
with pytest.raises(ValueError):
average([])The second test is the evidence. Without the guard, it fails with ZeroDivisionError. With the guard, it passes. The test now records a decision that was previously missing.
Check that the test can fail
A test that cannot fail proves nothing. Use this loop on any test you rely on:
- Run the new tests against the old code and confirm the right ones fail.
- Apply the change.
- Run them again and confirm they pass.
- Break the code on purpose, for example by deleting the guard, and confirm a test fails.
Step 4 is a quick form of mutation testing. If you can change the logic and nothing fails, the tests are not protecting that line.
git stash # set the AI's change aside
pytest # new tests should fail here
git stash pop # bring the change back
pytest # now they should passSpot weak tests
| Weak pattern | Why it is weak | What to ask for |
|---|---|---|
| Asserts only that no exception occurred | Passes for any return value | Assert the exact result |
| Mocks the function under test | Tests the mock | Mock only external systems |
| Copies the implementation into the expected value | Repeats the same bug | Use hand-computed values |
| Only one typical input | Misses boundaries | Add empty, zero, negative, and large cases |
| Test was edited to make it pass | Hides a regression | Review changes to existing tests first |
What passing tests do not cover
Tests only cover the cases someone thought of. They say little about security flaws, hallucinated dependencies, or fit with your codebase. Treat a green run as one item on the checklist, not as approval.
Exercise
An AI assistant adds a function percent(part, whole) and a test assert percent(1, 2) == 50.0. The test passes. Name one edge case it misses and what you would check before accepting.
Show answer
The test misses whole == 0, which raises ZeroDivisionError. Other gaps are negative values and non-numeric input. Decide the intended behavior from the requirement, add a test for it, run it against the old code to confirm it fails, then confirm it passes with the change. Also remove or alter the logic briefly to check that the existing test would catch a break.