Sub topic 8
Keeping each cycle small
This chapter assumes you can write and run unit tests with pytest, read a Git diff, and have used a coding agent. You have already seen the red, green and refactor phases. Here the focus is how much work you let one cycle hold.
Why size matters more with an agent
An agent can write a lot of code from one prompt. That is a risk in TDD. If a cycle covers several behaviors, a failing test no longer points to one cause. The diff is harder to review. When something breaks, you cannot tell which change did it.
A small cycle has three properties:
- It adds one failing test for one behavior.
- It makes one change that turns that test green.
- It ends with a passing suite and a commit.
One behavior per cycle
Take the slugify function from earlier chapters. A single prompt such as "write slugify with tests for lowercasing, punctuation, repeated separators and unicode" invites a large first draft. The agent may write the final implementation straight away. You then never see a test fail for the intended reason.
Split the request into cycles instead. Each cycle starts with one test:
# test_slugify.py
from slugify_util import slugify
def test_lowercases_and_joins_words_with_hyphens():
assert slugify("Hello World") == "hello-world"Once that is green and committed, add the next test on its own:
def test_strips_punctuation():
assert slugify("Hello, World!") == "hello-world"Run the suite. Only the new test should fail. If two tests fail, the cycle was too big or an earlier step left a problem behind.
python -m pytest -qSigns a cycle is too big
| Sign | What it usually means |
|---|---|
| The test name contains "and" | The test checks two behaviors. Split it. |
| Several tests fail at once after one change | You added more than one test, or the change touched too much. |
| The agent edits many files for one test | It is building ahead of the test. |
| The diff is longer than you can read in a few minutes | The step was too large to review well. |
| The agent adds code no test requires | It is guessing at future needs. |
Telling the agent to stay small
State the limit in the prompt and in the instruction file. Useful wording:
- "Write exactly one new failing test, then stop and show me the output."
- "Change only what the failing test requires."
- "Do not add features, parameters or error handling that no test asks for."
Stopping after the red step is a deliberate pause. It gives you a chance to check that the test fails for the right reason before any implementation exists.
What to do when a step is still too big
Sometimes the next test needs a large change. This happens when the existing design does not fit the new behavior. Do not push through. Try these in order:
- Pick a simpler test that moves toward the goal, such as a narrower input case.
- If the suite is green, refactor first to make room, then add the test.
- If the agent has already produced a large diff, discard it and restart from the last commit. The commit is your restore point.
The cycle as a loop with checkpoints
Exercise
Exercise
You ask an agent to add support for non-ASCII letters to slugify, and it returns a diff touching four files with three new tests, all passing. Name two problems with this step and say how you would restart.
Show answer
The step bundles several behaviors, so you never saw each test fail for its own reason, and the four-file diff is hard to review. Discard the changes with git restore . (or git checkout .) back to the last commit. Then ask for one failing test for a single input, confirm it fails because the behavior is missing, and repeat for each further behavior.