Skip to content
Agent loop · Testing

TDD Red-Green Loop

Build a feature test first: write one failing test per acceptance criterion, make it pass with minimal code, refactor, and move on to the next one.

Up to 12 iterationsStop rules and safety limits
The prompt

Copy the TDD Red-Green Loop loop

Stops when every acceptance criterion has a passing test and the full suite is green, or after 12 iterations. Edit the settings block, then paste the whole prompt into your agent.

tdd-red-green.prompt.md
You are running the "TDD Red-Green Loop".

Settings (edit before running):
- Feature spec: <paste the ticket, user story, or acceptance criteria here>
- Test command: npm test
- Max iterations (criteria cycles): 12

Goal: implement the spec one tested behavior at a time.

Before the loop: rewrite the spec as a numbered list of small, independently testable behaviors. Show the list and keep it updated.

Each iteration:
1. Red: write one test for the next unchecked behavior. Run it and confirm it fails, and that the failure message matches the missing behavior (not a typo or import error).
2. Green: write the smallest amount of production code that makes this test pass. Run the full suite.
3. Refactor: improve names and remove duplication in code you touched. Run the full suite again.
4. Check the behavior off the list and note the test name.

Stop when:
- Every behavior on the list is checked and the full suite passes. Success.
- You reach the max iteration count.
- A behavior is ambiguous or contradicts another. Stop and ask instead of guessing.

Rules:
- Never write production code for a behavior before its test has failed.
- Do not modify earlier tests to make a new one pass unless the spec changed. If so, say why.
- Keep one behavior per iteration. Do not batch.
- Do not add dependencies without saying why.

Final report: the checked list with test names, files changed, and any open questions about the spec.
When to use it

When this loop fits

  • You have a clear spec or ticket with acceptance criteria and want the agent to implement it safely.
  • You are adding behavior to fragile code and want each step locked in by a test.
  • You want an agent to produce code that is reviewable criterion by criterion.
How it works

What happens on each pass

The agent repeats these steps until the stop condition holds or it runs out of iterations.

  1. List the criteria. Turn the spec into a numbered list of small, testable behaviors.
  2. Red. Write one test for the next behavior and run it to confirm it fails for the expected reason.
  3. Green. Write the minimum code that makes the new test pass without breaking any other test.
  4. Refactor. Clean up duplication and naming while the suite stays green.
  5. Next criterion. Tick the criterion off and repeat until the list is done.
Recommended servers

MCP servers for this loop

The 6 strongest of 749 matching servers, ranked by how closely they fit and then by trust score.

More in Developer tools
  • Trust score 55 of 100, Fair

    Deterministic eligibility decisions and test-driven rule authoring via the Aethis developer API.

    Registry
    LicenseQualityMaintenanceLicense: Not graded. No license is recorded for this server.Quality: Not graded. No README or tool list has been captured yet.Maintenance: Not graded. No commit or release date has been recorded.
    — stars—/wk weekly downloads— tools
  • Trust score 55 of 100, Fair

    Checks whether your streaming redactor leaks a secret split across chunks; writes a pytest for it.

    Registry
    LicenseQualityMaintenanceLicense: Not graded. No license is recorded for this server.Quality: Not graded. No README or tool list has been captured yet.Maintenance: Not graded. No commit or release date has been recorded.
    — stars—/wk weekly downloads— tools
  • Trust score 50 of 100, Fair

    Elixium AI — XP-native agile platform. TDD-gated stories, team decisions, learning loop.

    Registry
    LicenseQualityMaintenanceLicense: Not graded. No license is recorded for this server.Quality: Not graded. No README or tool list has been captured yet.Maintenance: Not graded. No commit or release date has been recorded.
    — stars—/wk weekly downloads— tools
  • Trust score 49 of 100, Fair

    GodPrompt MCP + Agent Skill for coding agents with TDD, debugging, verification, and task routing.

    RegistryRemote
    LicenseQualityMaintenanceLicense: A. MIT is a permissive open source license.Quality: Not graded. No README or tool list has been captured yet.Maintenance: A. Last commit or release 11 days ago.
    1 stars2.3K/wk weekly downloads— tools
  • Trust score 46 of 100, Fair

    build

    by dave-london · TypeScript

    Auto-detects test framework (pytest, jest, vitest) and returns structured results.

    Registry
    LicenseQualityMaintenanceLicense: A. MIT is a permissive open source license.Quality: Not graded. No README or tool list has been captured yet.Maintenance: A. Last commit or release 10 days ago.
    139 stars11/wk weekly downloads— tools
  • Trust score 42 of 100, Fair

    Generates unit tests for Python code with coverage before/after reports and concrete edge cases.

    RegistryRemote
    LicenseQualityMaintenanceLicense: Not graded. No license is recorded for this server.Quality: Not graded. No README or tool list has been captured yet.Maintenance: Not graded. No commit or release date has been recorded.
    — stars—/wk weekly downloads— tools
Run it

How to run this loop

  1. Connect the tools. Install a server the loop can use, for example aethis-mcp, or rely on your agent's built-in terminal and file access.
  2. Edit the settings block. Replace the example commands, targets, and limits with your project's own. Keep the iteration cap low on the first run.
  3. Paste it into an agent session. Start a fresh session in Claude Code, Cursor, or VS Code agent mode, in the repository root, and paste the prompt.
  4. Review before you merge. Read the final report and the diff. The loop leaves its work for you to review rather than shipping it.
FAQ

TDD Red-Green Loop questions

When does the TDD Red-Green Loop loop stop?

It stops as soon as every acceptance criterion has a passing test and the full suite is green. It also stops after 12 iterations by default, or earlier when the prompt's own stop rules say it is stuck, and then reports what changed and what is left. Change the cap in the settings block at the top of the prompt.

Why use test-driven development with a coding agent?

Agents are good at making code pass a test and bad at knowing when they are done. Writing the failing test first gives each step a clear finish line, keeps changes small, and leaves a test suite that documents exactly what was built.

Why must the test fail for the expected reason?

A test that fails because of a typo or a missing import proves nothing about the feature. Checking the failure message confirms that the test really exercises the missing behavior, so its later pass actually means something.

Keep exploring
All loops