CClaude Code Catalog
All Patterns

Test-Driven Development with Agents

AgentIntermediate

Classic TDD follows Red-Green-Refactor. This pattern automates it with two Claude Code agents: Agent A writes a failing test based on the spec, then Agent B implements the minimum code to make it pass. The cycle repeats for each requirement. A third optional agent handles the refactor phase. This enforces strict TDD discipline and produces well-tested code by design.

tddtestingagentred-green-refactor

Pattern Code

# TDD Agent Pattern ## CLAUDE.md — Test Writer Agent ```markdown # Role: Test Writer You write ONLY test files. Never write implementation code. ## Rules - Read the spec/requirement first - Write one test at a time (smallest meaningful assertion) - Use descriptive test names: "should [expected behavior] when [condition]" - Import from the implementation file even though it doesn't exist yet - After writing a test, run it to confirm it FAILS (red phase) - Mark test with // SPEC: [requirement-id] for traceability ## Test Style - Framework: Vitest - Pattern: Arrange-Act-Assert - Mocking: vi.mock() for external deps only ``` ## CLAUDE.md — Implementer Agent ```markdown # Role: Implementer You write ONLY implementation code. Never modify test files. ## Rules - Read the failing test first - Write the MINIMUM code to make the test pass - Do not add functionality beyond what the test requires - Run the test after each change to confirm it PASSES (green phase) - Do not optimize or refactor — that's a separate phase ## Implementation Style - Follow existing patterns in the codebase - Export only what the test imports - Prefer pure functions where possible ``` ## Orchestration ```bash #!/bin/bash # tdd-cycle.sh — automated TDD loop SPEC_FILE=$1 REQUIREMENTS=$(claude -p "Extract requirement IDs from $SPEC_FILE as JSON array") for REQ in $(echo $REQUIREMENTS | jq -r '.[]'); do echo "=== Requirement: $REQ ===" # Red: Write failing test claude -p "Write a failing test for requirement $REQ from $SPEC_FILE. Follow Test Writer rules in CLAUDE.md." # Green: Implement to pass claude -p "Read the failing test for $REQ. Write minimum code to make it pass. Follow Implementer rules in CLAUDE.md." # Verify npx vitest run --reporter=verbose done # Refactor phase (optional) claude -p "Review all implementation files. Refactor for clarity without changing behavior. All tests must still pass." ```

Copy this pattern into your project configuration to implement.

Terminal Preview

Test-Driven Development with Agents

About Test-Driven Development with Agents

Claude Code patterns are proven architectural designs and workflow structures that help you tackle complex development scenarios. Test-Driven Development with Agents is a Agent pattern at the Intermediate level that provides a tested, repeatable approach you can adapt to your projects for more efficient and consistent results.

Related Patterns