TDD Cycle - Comprehensive Test-Driven Development
Phases
Run the full RED → GREEN → REFACTOR cycle, or one phase at a time with --phase red|green|refactor (--phase all is the default).
Examples
# Full cycle (default)
tdd-cycle "user authentication"
# Single phases
tdd-cycle --phase red "user authentication"
tdd-cycle --phase green "user authentication"
tdd-cycle --phase refactor "user authentication"
# Same as default — explicit
tdd-cycle --phase all "user authentication"
Execute a comprehensive Test-Driven Development (TDD) workflow with strict red-green-refactor discipline:
[Extended thinking: This workflow enforces test-first development through coordinated agent orchestration. Each phase of the TDD cycle is strictly enforced with fail-first verification, incremental implementation, and continuous refactoring. The workflow supports both single test and test suite approaches with configurable coverage thresholds.]
Configuration
Coverage Thresholds
- Minimum line coverage: 80%
- Minimum branch coverage: 75%
- Critical path coverage: 100%
Refactoring Triggers
- Cyclomatic complexity > 10
- Method length > 20 lines
- Class length > 200 lines
- Duplicate code blocks > 3 lines
Phase 1: Test Specification and Design
1. Requirements Analysis
- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.
- Prompt: "Analyze requirements for: $ARGUMENTS. Define acceptance criteria, identify edge cases, and create test scenarios. Output a comprehensive test specification."
- Output: Test specification, acceptance criteria, edge case matrix
- Validation: Ensure all requirements have corresponding test scenarios
2. Test Architecture Design
- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.
- Prompt: "Design test architecture for: $ARGUMENTS based on test specification. Define test structure, fixtures, mocks, and test data strategy. Ensure testability and maintainability."
- Output: Test architecture, fixture design, mock strategy
- Validation: Architecture supports isolated, fast, reliable tests
Phase 2: RED - Write Failing Tests
3. Write Unit Tests (Failing)
- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.
- Prompt: "Write FAILING unit tests for: $ARGUMENTS. Tests must fail initially. Include edge cases, error scenarios, and happy paths. DO NOT implement production code."
- Output: Failing unit tests, test documentation
- CRITICAL: Verify all tests fail with expected error messages
4. Verify Test Failure
- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.
- Prompt: "Verify that all tests for: $ARGUMENTS are failing correctly. Ensure failures are for the right reasons (missing implementation, not test errors). Confirm no false positives."
- Output: Test failure verification report
- GATE: Do not proceed until all tests fail appropriately
Phase 3: GREEN - Make Tests Pass
5. Minimal Implementation
- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.
- Prompt: "Implement MINIMAL code to make tests pass for: $ARGUMENTS. Focus only on making tests green. Do not add extra features or optimizations. Keep it simple."
- Output: Minimal working implementation
- Constraint: No code beyond what's needed to pass tests
6. Verify Test Success
- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.
- Prompt: "Run all tests for: $ARGUMENTS and verify they pass. Check test coverage metrics. Ensure no tests were accidentally broken."
- Output: Test execution report, coverage metrics
- GATE: All tests must pass before proceeding
Phase 4: REFACTOR - Improve Code Quality
7. Code Refactoring
- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.
- Prompt: "Refactor implementation for: $ARGUMENTS while keeping tests green. Apply SOLID principles, remove duplication, improve naming, and optimize performance. Run tests after each refactoring."
- Output: Refactored code, refactoring report
- Constraint: Tests must remain green throughout
8. Test Refactoring
- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.
- Prompt: "Refactor tests for: $ARGUMENTS. Remove test duplication, improve test names, extract common fixtures, and enhance test readability. Ensure tests still provide same coverage."
- Output: Refactored tests, improved test structure
- Validation: Coverage metrics unchanged or improved
Phase 5: Integration and System Tests
9. Write Integration Tests (Failing First)
- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.
- Prompt: "Write FAILING integration tests for: $ARGUMENTS. Test component interactions, API contracts, and data flow. Tests must fail initially."
- Output: Failing integration tests
- Validation: Tests fail due to missing integration logic
10. Implement Integration
- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.
- Prompt: "Implement integration code for: $ARGUMENTS to make integration tests pass. Focus on component interaction and data flow."
- Output: Integration implementation
- Validation: All integration tests pass
Phase 6: Continuous Improvement Cycle
11. Performance and Edge Case Tests
- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.
- Prompt: "Add performance tests and additional edge case tests for: $ARGUMENTS. Include stress tests, boundary tests, and error recovery tests."
- Output: Extended test suite
- Metric: Increased test coverage and scenario coverage
12. Final Code Review
- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.
- Prompt: "Perform comprehensive review of: $ARGUMENTS. Verify TDD process was followed, check code quality, test quality, and coverage. Suggest improvements."
- Output: Review report, improvement suggestions
- Action: Implement critical suggestions while maintaining green tests
Incremental Development Mode
For test-by-test development:
- Write ONE failing test
- Make ONLY that test pass
- Refactor if needed
- Repeat for next test
Use this approach by adding --incremental flag to focus on one test at a time.
Test Suite Mode
For comprehensive test suite development:
- Write ALL tests for a feature/module (failing)
- Implement code to pass ALL tests
- Refactor entire module
- Add integration tests
Use this approach by adding --suite flag for batch test development.
Validation Checkpoints
RED Phase Validation
GREEN Phase Validation
REFACTOR Phase Validation
Coverage Reports
Generate coverage reports after each phase:
- Line coverage
- Branch coverage
- Function coverage
- Statement coverage
Failure Recovery
If TDD discipline is broken:
- STOP immediately
- Identify which phase was violated
- Rollback to last valid state
- Resume from correct phase
- Document lesson learned
TDD Metrics Tracking
Track and report:
- Time in each phase (Red/Green/Refactor)
- Number of test-implementation cycles
- Coverage progression
- Refactoring frequency
- Defect escape rate
Anti-Patterns to Avoid
- Writing implementation before tests
- Writing tests that already pass
- Skipping the refactor phase
- Writing multiple features without tests
- Modifying tests to make them pass
- Ignoring failing tests
- Writing tests after implementation
Anti-Rationalization Table
These excuses signal you're about to break TDD discipline. Recognize them and return to the correct phase.
| Excuse |
Rebuttal |
Why It Matters |
| "I'll test after" |
Tests written after implementation pass immediately, proving nothing. You can't verify a test catches bugs if you never saw it fail. |
Tests-after are biased by implementation — you test what you built, not what's required |
| "This is too simple to test" |
Simple code has the most unexamined assumptions. Simple code breaks at integration boundaries where you least expect it. |
The simpler it seems, the more likely you'll miss an edge case |
| "TDD slows me down" |
TDD prevents 2-3 hour debugging sessions. Writing a test takes minutes; debugging an untested bug takes hours. |
The time you "save" skipping tests is borrowed against future debugging |
| "Just this once" |
Every "just this once" becomes the new standard. Discipline has no exceptions — the exception IS the precedent. |
One skip normalizes skipping. There is no "just this once" |
| "I already manually tested it" |
Manual testing leaves no record, isn't repeatable, and doesn't catch regressions. It proves nothing to anyone but you, right now. |
Manual tests evaporate. Automated tests compound |
| "Deleting my code is wasteful" |
Sunk cost fallacy — keeping bad code costs more than writing it correctly. Code written without tests is unverified code. |
Bad code costs more to maintain than to rewrite correctly |
| "The spirit matters, not the ritual" |
The ritual IS the spirit — the specific order (test → fail → implement → pass → refactor) is what produces the quality guarantee. |
Skipping steps while claiming to follow TDD is not following TDD |
| "I'll keep deleted code as reference" |
Reference code biases you toward the wrong implementation. Start fresh — the test tells you what to write. |
Prior code anchors you to assumptions that may be wrong |
Success Criteria
- 100% of code written test-first
- All tests pass continuously
- Coverage exceeds thresholds
- Code complexity within limits
- Zero defects in covered code
- Clear test documentation
- Fast test execution (< 5 seconds for unit tests)
Notes
- Enforce strict RED-GREEN-REFACTOR discipline
- Each phase must be completed before moving to next
- Tests are the specification
- If a test is hard to write, the design needs improvement
- Refactoring is NOT optional
- Keep test execution fast
- Tests should be independent and isolated
TDD implementation for: $ARGUMENTS
Resources
1---2name: tdd-cycle3description: Execute full TDD red-green-refactor cycle with validation gates. Use when saying "TDD cycle", "test-driven development", or "full TDD workflow".4---56# TDD Cycle - Comprehensive Test-Driven Development78## Phases910Run the full RED → GREEN → REFACTOR cycle, or one phase at a time with `--phase red|green|refactor` (`--phase all` is the default).1112### Examples1314```bash15# Full cycle (default)16tdd-cycle "user authentication"1718# Single phases19tdd-cycle --phase red "user authentication"20tdd-cycle --phase green "user authentication"21tdd-cycle --phase refactor "user authentication"2223# Same as default — explicit24tdd-cycle --phase all "user authentication"25```2627---2829Execute a comprehensive Test-Driven Development (TDD) workflow with strict red-green-refactor discipline:3031[Extended thinking: This workflow enforces test-first development through coordinated agent orchestration. Each phase of the TDD cycle is strictly enforced with fail-first verification, incremental implementation, and continuous refactoring. The workflow supports both single test and test suite approaches with configurable coverage thresholds.]3233## Configuration3435### Coverage Thresholds36- Minimum line coverage: 80%37- Minimum branch coverage: 75%38- Critical path coverage: 100%3940### Refactoring Triggers41- Cyclomatic complexity > 1042- Method length > 20 lines43- Class length > 200 lines44- Duplicate code blocks > 3 lines4546## Phase 1: Test Specification and Design4748### 1. Requirements Analysis49- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.50- Prompt: "Analyze requirements for: $ARGUMENTS. Define acceptance criteria, identify edge cases, and create test scenarios. Output a comprehensive test specification."51- Output: Test specification, acceptance criteria, edge case matrix52- Validation: Ensure all requirements have corresponding test scenarios5354### 2. Test Architecture Design55- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.56- Prompt: "Design test architecture for: $ARGUMENTS based on test specification. Define test structure, fixtures, mocks, and test data strategy. Ensure testability and maintainability."57- Output: Test architecture, fixture design, mock strategy58- Validation: Architecture supports isolated, fast, reliable tests5960## Phase 2: RED - Write Failing Tests6162### 3. Write Unit Tests (Failing)63- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.64- Prompt: "Write FAILING unit tests for: $ARGUMENTS. Tests must fail initially. Include edge cases, error scenarios, and happy paths. DO NOT implement production code."65- Output: Failing unit tests, test documentation66- **CRITICAL**: Verify all tests fail with expected error messages6768### 4. Verify Test Failure69- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.70- Prompt: "Verify that all tests for: $ARGUMENTS are failing correctly. Ensure failures are for the right reasons (missing implementation, not test errors). Confirm no false positives."71- Output: Test failure verification report72- **GATE**: Do not proceed until all tests fail appropriately7374## Phase 3: GREEN - Make Tests Pass7576### 5. Minimal Implementation77- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.78- Prompt: "Implement MINIMAL code to make tests pass for: $ARGUMENTS. Focus only on making tests green. Do not add extra features or optimizations. Keep it simple."79- Output: Minimal working implementation80- Constraint: No code beyond what's needed to pass tests8182### 6. Verify Test Success83- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.84- Prompt: "Run all tests for: $ARGUMENTS and verify they pass. Check test coverage metrics. Ensure no tests were accidentally broken."85- Output: Test execution report, coverage metrics86- **GATE**: All tests must pass before proceeding8788## Phase 4: REFACTOR - Improve Code Quality8990### 7. Code Refactoring91- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.92- Prompt: "Refactor implementation for: $ARGUMENTS while keeping tests green. Apply SOLID principles, remove duplication, improve naming, and optimize performance. Run tests after each refactoring."93- Output: Refactored code, refactoring report94- Constraint: Tests must remain green throughout9596### 8. Test Refactoring97- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.98- Prompt: "Refactor tests for: $ARGUMENTS. Remove test duplication, improve test names, extract common fixtures, and enhance test readability. Ensure tests still provide same coverage."99- Output: Refactored tests, improved test structure100- Validation: Coverage metrics unchanged or improved101102## Phase 5: Integration and System Tests103104### 9. Write Integration Tests (Failing First)105- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.106- Prompt: "Write FAILING integration tests for: $ARGUMENTS. Test component interactions, API contracts, and data flow. Tests must fail initially."107- Output: Failing integration tests108- Validation: Tests fail due to missing integration logic109110### 10. Implement Integration111- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.112- Prompt: "Implement integration code for: $ARGUMENTS to make integration tests pass. Focus on component interaction and data flow."113- Output: Integration implementation114- Validation: All integration tests pass115116## Phase 6: Continuous Improvement Cycle117118### 11. Performance and Edge Case Tests119- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.120- Prompt: "Add performance tests and additional edge case tests for: $ARGUMENTS. Include stress tests, boundary tests, and error recovery tests."121- Output: Extended test suite122- Metric: Increased test coverage and scenario coverage123124### 12. Final Code Review125- Run this step in a fresh context (a subagent, or a new session) so earlier reasoning cannot leak into it.126- Prompt: "Perform comprehensive review of: $ARGUMENTS. Verify TDD process was followed, check code quality, test quality, and coverage. Suggest improvements."127- Output: Review report, improvement suggestions128- Action: Implement critical suggestions while maintaining green tests129130## Incremental Development Mode131132For test-by-test development:1331. Write ONE failing test1342. Make ONLY that test pass1353. Refactor if needed1364. Repeat for next test137138Use this approach by adding `--incremental` flag to focus on one test at a time.139140## Test Suite Mode141142For comprehensive test suite development:1431. Write ALL tests for a feature/module (failing)1442. Implement code to pass ALL tests1453. Refactor entire module1464. Add integration tests147148Use this approach by adding `--suite` flag for batch test development.149150## Validation Checkpoints151152### RED Phase Validation153- [ ] All tests written before implementation154- [ ] All tests fail with meaningful error messages155- [ ] Test failures are due to missing implementation156- [ ] No test passes accidentally157158### GREEN Phase Validation159- [ ] All tests pass160- [ ] No extra code beyond test requirements161- [ ] Coverage meets minimum thresholds162- [ ] No test was modified to make it pass163164### REFACTOR Phase Validation165- [ ] All tests still pass after refactoring166- [ ] Code complexity reduced167- [ ] Duplication eliminated168- [ ] Performance improved or maintained169- [ ] Test readability improved170171## Coverage Reports172173Generate coverage reports after each phase:174- Line coverage175- Branch coverage176- Function coverage177- Statement coverage178179## Failure Recovery180181If TDD discipline is broken:1821. **STOP** immediately1832. Identify which phase was violated1843. Rollback to last valid state1854. Resume from correct phase1865. Document lesson learned187188## TDD Metrics Tracking189190Track and report:191- Time in each phase (Red/Green/Refactor)192- Number of test-implementation cycles193- Coverage progression194- Refactoring frequency195- Defect escape rate196197## Anti-Patterns to Avoid198199- Writing implementation before tests200- Writing tests that already pass201- Skipping the refactor phase202- Writing multiple features without tests203- Modifying tests to make them pass204- Ignoring failing tests205- Writing tests after implementation206207## Anti-Rationalization Table208209These excuses signal you're about to break TDD discipline. Recognize them and return to the correct phase.210211| Excuse | Rebuttal | Why It Matters |212|--------|----------|----------------|213| "I'll test after" | Tests written after implementation pass immediately, proving nothing. You can't verify a test catches bugs if you never saw it fail. | Tests-after are biased by implementation — you test what you built, not what's required |214| "This is too simple to test" | Simple code has the most unexamined assumptions. Simple code breaks at integration boundaries where you least expect it. | The simpler it seems, the more likely you'll miss an edge case |215| "TDD slows me down" | TDD prevents 2-3 hour debugging sessions. Writing a test takes minutes; debugging an untested bug takes hours. | The time you "save" skipping tests is borrowed against future debugging |216| "Just this once" | Every "just this once" becomes the new standard. Discipline has no exceptions — the exception IS the precedent. | One skip normalizes skipping. There is no "just this once" |217| "I already manually tested it" | Manual testing leaves no record, isn't repeatable, and doesn't catch regressions. It proves nothing to anyone but you, right now. | Manual tests evaporate. Automated tests compound |218| "Deleting my code is wasteful" | Sunk cost fallacy — keeping bad code costs more than writing it correctly. Code written without tests is unverified code. | Bad code costs more to maintain than to rewrite correctly |219| "The spirit matters, not the ritual" | The ritual IS the spirit — the specific order (test → fail → implement → pass → refactor) is what produces the quality guarantee. | Skipping steps while claiming to follow TDD is not following TDD |220| "I'll keep deleted code as reference" | Reference code biases you toward the wrong implementation. Start fresh — the test tells you what to write. | Prior code anchors you to assumptions that may be wrong |221222## Success Criteria223224- 100% of code written test-first225- All tests pass continuously226- Coverage exceeds thresholds227- Code complexity within limits228- Zero defects in covered code229- Clear test documentation230- Fast test execution (< 5 seconds for unit tests)231232## Notes233234- Enforce strict RED-GREEN-REFACTOR discipline235- Each phase must be completed before moving to next236- Tests are the specification237- If a test is hard to write, the design needs improvement238- Refactoring is NOT optional239- Keep test execution fast240- Tests should be independent and isolated241242TDD implementation for: $ARGUMENTS243244## Resources245246- [Official Documentation](https://verified-skill.com/docs/reference/skills#tdd-cycle)