---
name: test-driven-development-loop
description: "Drives a change with red, green, refactor: a failing test seen to fail for the right reason comes before any production code. Use when a new behavior or bug fix should be built test first, with a failing test proven before the implementation. Do not use for adding tests to code that already works or choosing a testing strategy. Related topics: coding, technical."
---

# Test-Driven Development Loop

Drives a change with red, green, refactor: a failing test seen to fail for the right reason comes before any production code.

## When to use

Use this skill when a new behavior or bug fix should be built test first, with a failing test proven before the implementation. Do not use it for adding tests to code that already works or choosing a testing strategy.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Write one test for the smallest next slice of behavior and run it before touching production code.
3. Read the failure and confirm it fails because the behavior is absent; fix the test until the reason is right.
4. Write the least code that passes, then run the whole test file to confirm nothing else broke.
5. Refactor only while green, running the tests after every edit, to the chosen appetite.
6. Repeat from the simplest case outward to edge and error cases, logging each cycle in three lines.

## Inputs

- **Behavior to add** (longer free text)
- **Language and test framework** (free text, e.g. "Go with testing or Ruby with RSpec")
- **Test command** (free text, e.g. "The command that runs one test file")
- **Relevant code** (longer free text)
- **Refactor appetite** (choose one: Minimal, Moderate, Thorough)

## Output

A cycle log of test written, failure observed and code added, ending with the final passing test run and a note on what was refactored.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Test-Driven Development Loop

Implement this behavior using strict test-driven development.

- Behavior to add: {{Behavior to add}}
- Language and test framework: {{Language and test framework}}
- Test command: {{Test command}}
- Relevant code: {{Relevant code}}
- Refactor appetite: {{Refactor appetite}}

Work in small cycles. Red: write one test for the next smallest slice of behavior, run it, and read the failure. Confirm it fails because the behavior is missing, not because of a typo or import error; fix the test until the failure message is the right one. Green: write the least production code that makes this test pass and nothing more, then run the whole file. Refactor: with the tests green, remove duplication and improve names to the chosen appetite, running the tests after each edit. Repeat for the next slice, starting with the simplest case and growing to edge cases and error paths. Never write production code without a failing test that demands it. Report each cycle in three lines: the test, the failure seen, the code added.

## Pitfalls

- Accepting a test that fails on a syntax or import error, which proves nothing about the behavior.
- Writing the full implementation in one go after the first test, which skips the tests that would have shaped it.
- Refactoring while a test is red, so a regression cannot be told from the change.
