Topic 3 of 5 · 4 min read
The red, green, review cycle
Goal: Run a red-green-review cycle and confirm each test failed for the expected reason before the agent made it pass.
Run the cycle in three steps: red (the test fails, for the reason you expect), green (the agent makes it pass with the least code), review (you read the change and clean it up). The step people skip is reading why the test was red.
Check yourself · 1 of 3
In the red step, what do you check?
Why it matters
A failing test is not always a useful one. It can fail because of a typo, a missing import or a wrong file path. When the agent then makes it pass, you have proven only that the typo is fixed. Martin Fowler describes TDD as a loop of failing test, passing code and refactoring in small steps. With an agent the steps are the same, but the agent moves fast enough to do many at once. You slow it down on purpose.
How to do it
- Red. Run the new test before there is any implementation. Read the failure message. For
discount(100)it should say "expected 90, received undefined" or "discount is not a function". It should not say "cannot find module" from a wrong path. If the reason is wrong, fix the test, not the code. - Green. Give the agent one failing test or a small group. Ask for the simplest code that passes, and ask it to run the tests and show the output. Do not ask for "the whole feature" in one go.
- Review. Read the diff. Is it only what the test needs? Did it touch the tests? Did it add a special case for the test's exact inputs? Run the full suite and not only the new test.
- Refactor. With green tests as a safety net, ask for a cleanup: remove duplication, rename, simplify. The tests must stay green and unchanged.
- Repeat with the next test.
Before and after
Weak: the agent is told "implement the shipping module" with 14 failing tests. It changes six files, and two tests turn green by accident while the others fail for new reasons.
Better:
Run the failing test "free shipping over 50" and paste the failure.
Then write the smallest change in src/shipping.ts that makes it pass.
Do not edit tests. Run the whole suite and paste the summary line.
You see "expected 0, received 4.99", which is the right reason. After the change, one test goes green and none go red.
What goes wrong
- Red for the wrong reason. The test fails on setup, so green proves nothing.
- Too big a step. Many tests at once give a large diff that is hard to review.
- Skipping the full run. The new test passes while an older one now fails.
- Skipping refactor. Agent code that only passes the test tends to pile up. Clean it while the tests protect you.
Examples
Wrong red
A new test fails with "Cannot find module './shipping'". That is a file path problem, not a missing behaviour. Create an empty module with the function stub, run again, and look for a failure that says the result was wrong.
Small green step
Three tests are red. You hand the agent only the first and say: "Make this one pass, nothing else." The diff is six lines. Next you hand it the second, and the rule it adds there is visible in the diff instead of buried in a large one.
Key points
- Red means the test fails for the reason you expect, and you read the failure message to check.
- Green means the smallest change that passes, asked for in small steps and not a whole feature.
- Review means reading the diff for test edits, special cases and unrelated changes.
- Always run the full suite after green, since a new pass can hide an old failure.
- Refactor with the tests green and unchanged, so the cleanup is safe.
Common mistakes
- Not reading the failure message, so a test that fails on a typo counts as red.
- Handing the agent every failing test at once and receiving a diff nobody can review.
- Running only the new test and missing a regression elsewhere.
- Skipping the refactor step because the tests are green.
Check yourself · 2 of 3
The agent made the new test pass. What should you run next?
Check yourself · 3 of 3
Fourteen tests are red. How should you hand them to the agent?
Practice items, not counted towards your mastery.
Start here
- Canon TDD (Kent Beck) (opens in a new tab)ArticleFree
The author of TDD lists the steps: write a test list, one test at a time, make it pass, refactor.
- Test Driven Development (martinfowler.com) (opens in a new tab)ArticleFree
A short definition of the red, green, refactor loop and why it keeps changes small.
- Best practices for Claude Code (Claude Code docs) (opens in a new tab)DocsFree
A vendor's advice on giving agents tests and checks to verify their own work.