Sub topic 5
Red: write a failing test and check why it fails
This chapter covers the first step of the cycle: writing one failing test and confirming it fails for the right reason. It assumes you can write and run unit tests in Python with pytest, read a Git diff, and have used a coding agent. We use the slugify function from the earlier examples.
Why red comes first
A test that has never failed has never been shown to detect anything. If you ask an agent to write the test and the code together, you can end up with a test that passes whether or not the behavior exists. Running the test before any implementation exists proves the test can fail. It also shows what the failure looks like, which is the signal the agent will use in the next step.
For an agent, the red test is the specification. The agent reads the failure output, finds the gap between expected and actual behavior, and works to close it.
Write one test for one behavior
Pick a single behavior and name the test after it. The name should read as a sentence about the behavior.
# test_slugify.py
from slugify_util import slugify
def test_lowercases_and_joins_words_with_hyphens():
assert slugify("Hello World") == "hello-world"Keep the first test small. One assertion, one behavior. If the test names two behaviors, the agent has to guess which to build first, and a failure will not tell you which one is missing.
Run it and read the failure
Run the suite with the command the agent will also use:
python -m pytest -qAt this point slugify_util.py does not exist. The output ends with something like this:
ImportError while importing test module 'test_slugify.py'.
E ModuleNotFoundError: No module named 'slugify_util'This is a failure, but it is the wrong kind. The test did not run its assertion. It failed because of a missing module, which says nothing about whether the assertion is correct.
Distinguish the right failure from the wrong one
A good red has one property: the test reaches its assertion and the assertion fails because the behavior is missing. To get there, create a stub that exists but does nothing useful.
# slugify_util.py
def slugify(text):
raise NotImplementedErrorNow run the test again. A still-wrong failure would be a NotImplementedError, because the code never produced a value to compare. A stub that returns the input unchanged gives a better red:
# slugify_util.py
def slugify(text):
return textE AssertionError: assert 'Hello World' == 'hello-world'This is the failure you want. The test ran, the function returned a value, and the value differs from the expectation in the way the missing behavior would cause.
| Failure | What it means | Valid red? |
|---|---|---|
| `ModuleNotFoundError` or `ImportError` | Setup problem, no assertion ran | No |
| `SyntaxError` or `NameError` in the test | Typo in the test | No |
| `NotImplementedError` | Stub reached, no value compared | Weak |
| `AssertionError` with a wrong value | Behavior is missing | Yes |
Make the agent check the reason
Agents tend to move straight from writing a test to writing code. Tell them to stop and report why the test failed. The working agreement from earlier chapters includes this step:
1. Work test-first. Write one failing test for the requested behavior and run it.
2. Show the failure output and confirm it fails because the behavior is missing, not because of a typo or import error.Read the output yourself as well. Ask two questions. Does the failure message mention the behavior you described? Would the test pass if the behavior were correctly implemented? If the answer to either is unclear, fix the test before any implementation begins.
Common errors
- A test that passes immediately. Either the behavior already exists or the assertion is too weak. Do not continue until you know which.
- Several new tests at once. The failure output mixes causes. Add one test per cycle.
- The agent edits the test to get a clearer failure. Fixing a typo is fine. Changing the expected value after seeing the output is not. Review the diff for this.
- Asserting on the wrong thing. A test such as
assert slugify("Hello World")with no comparison always passes for a non-empty string. Check that each assertion compares a result with an explicit expected value.
Exercise
You write a new test and run it. The output shows NameError: name 'slugfy' is not defined raised inside the test file. Is this a valid red, and what do you do next?
Show answer
It is not a valid red. The test has a typo in the function name, so no assertion ran. Fix the typo, rerun, and check that the failure is now an AssertionError showing a wrong value for the missing behavior. Only then ask the agent to implement.
Exercise
A new test passes on its first run, before any implementation change. Give two possible explanations and one way to tell them apart.
Show answer
Either the behavior already exists in the code, or the assertion is too weak to detect its absence. To tell them apart, temporarily break the relevant code, or change the expected value in the assertion, and rerun. If the test still passes after the code is broken, the test is too weak.