Sub topic 6
Green: the smallest change that passes
This chapter assumes you can write and run pytest tests, read a Git diff, and have used a coding agent. It covers the green phase of the red-green-refactor cycle, where you get a failing test to pass with the least code possible.
What green means
Green is the phase after a test fails for the right reason. The goal is narrow: make that one test pass without breaking any other test. You are not trying to write the final design. You are trying to get a trustworthy passing state that you can build on.
"Smallest" matters for an agent. Left alone, an agent tends to implement the whole feature, handle cases nobody asked for, and add helpers you must now review. Each extra line is behavior that no test covers.
A worked cycle
The red test from the previous phase states one behavior:
# test_slugify.py
from slugify_util import slugify
def test_lowercases_and_joins_words_with_hyphens():
assert slugify("Hello World") == "hello-world"The smallest implementation that passes it is:
# slugify_util.py
def slugify(text):
return text.lower().replace(" ", "-")This code ignores punctuation and repeated spaces. That is intentional. No test requires those behaviors yet, so the code does not provide them. They will arrive in later cycles, each one driven by its own failing test.
Run the suite and read the result:
python -m pytest -qThe output is the agent's feedback. A passing run ends with a line such as 1 passed. If the agent sees a failure, it reads the message, edits the code, and runs again. Always run the full suite, not only the new test, so you notice when a change breaks earlier behavior.
Instructing the agent
Tell the agent the scope explicitly. A prompt that works well:
- Name the failing test.
- Ask for the smallest change that makes it pass.
- Forbid edits to test files.
- Ask it to run the full suite and report the output.
The working agreement in your repository can carry these rules permanently. The relevant lines are "write the smallest change that makes that test pass, then run the full test suite" and "do not hardcode expected values from tests into the implementation."
Small does not mean fake
There is one way to be too small. An agent can pass a test by returning the expected value directly:
# A degenerate 'green' an agent might produce
def slugify(text):
return "hello-world"This passes the first test. It also solves nothing. Hardcoding is a legitimate technique only when you know the next test will force a real implementation. With one example, you cannot tell the difference between a real solution and a lookup of the answer.
The remedy is more varied tests, not a bigger implementation. Add a second test with different input and the hardcoded version fails:
# test_slugify.py (added in the next cycles)
def test_strips_punctuation():
assert slugify("Hello, World!") == "hello-world"
def test_collapses_repeated_separators():
assert slugify("a -- b") == "a-b"Both new tests fail against the simple replace version, so each justifies a real change.
Common errors
| Symptom | Likely cause | Response |
|---|---|---|
| Agent implements several behaviors at once | Prompt did not limit scope | Revert and re-prompt with one test named |
| Test passes because the expected value appears in the code | Hardcoding | Add a test with different input |
| Test passes after the agent edited the test | Agent changed the specification | Reject the diff and restore the test |
| New test passes but an old one fails | Change was not checked against the full suite | Run the whole suite every time |
Finishing the phase
Green is done when the full suite passes and the diff contains only what that one test required. Commit at this point, or move straight to refactoring while the suite stays green. Do not start the next failing test until the current state is passing.
Exercise
A test asserts that slugify("Hello World") == "hello-world". The agent submits return "hello-world" and the test passes. Why is this not an acceptable green, and what is the next step?
Show answer
The function ignores its input, so it only satisfies the one example. Passing proves nothing about the behavior. The next step is to add another failing test with different input, such as slugify("Hello, World!"), so the implementation has to do real work. Then ask the agent for the smallest general change that passes both tests.
Exercise
Why should the agent run the full suite after making a test pass, instead of only the new test?
Show answer
A change that fixes the new test can break earlier behavior. Only the full suite reveals that regression, and its output is the feedback the agent uses to decide its next edit.