TDD Cycle - Comprehensive Test-Driven Development
Project Overrides
!s="tdd-cycle"; for d in .specweave/skill-memories .claude/skill-memories "$HOME/.claude/skill-memories"; do p="$d/$s.md"; [ -f "$p" ] && awk '/^## Learnings$/{ok=1;next}/^## /{ok=0}ok' "$p" && break; done 2>/dev/null; true
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
- Use Task tool with subagent_type="Plan"
- 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
- Use Task tool with subagent_type="general-purpose"
- 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)
- Use Task tool with subagent_type="general-purpose"
- 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
- Use Task tool with subagent_type="general-purpose"
- 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
- Use Task tool with subagent_type="general-purpose"
- 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
- Use Task tool with subagent_type="general-purpose"
- 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
- Use Task tool with subagent_type="general-purpose"
- 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
- Use Task tool with subagent_type="general-purpose"
- 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)
- Use Task tool with subagent_type="general-purpose"
- 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
- Use Task tool with subagent_type="general-purpose"
- 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
- Use Task tool with subagent_type="general-purpose"
- 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
- Use Task tool with subagent_type="Plan"
- 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
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
1---2name: tdd-cycle-anton-abyzov-specweave3description: 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## Project Overrides910!`s="tdd-cycle"; for d in .specweave/skill-memories .claude/skill-memories "$HOME/.claude/skill-memories"; do p="$d/$s.md"; [ -f "$p" ] && awk '/^## Learnings$/{ok=1;next}/^## /{ok=0}ok' "$p" && break; done 2>/dev/null; true`1112Execute a comprehensive Test-Driven Development (TDD) workflow with strict red-green-refactor discipline:1314[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.]1516## Configuration1718### Coverage Thresholds19- Minimum line coverage: 80%20- Minimum branch coverage: 75%21- Critical path coverage: 100%2223### Refactoring Triggers24- Cyclomatic complexity > 1025- Method length > 20 lines26- Class length > 200 lines27- Duplicate code blocks > 3 lines2829## Phase 1: Test Specification and Design3031### 1. Requirements Analysis32- Use Task tool with subagent_type="Plan"33- Prompt: "Analyze requirements for: $ARGUMENTS. Define acceptance criteria, identify edge cases, and create test scenarios. Output a comprehensive test specification."34- Output: Test specification, acceptance criteria, edge case matrix35- Validation: Ensure all requirements have corresponding test scenarios3637### 2. Test Architecture Design38- Use Task tool with subagent_type="general-purpose"39- Prompt: "Design test architecture for: $ARGUMENTS based on test specification. Define test structure, fixtures, mocks, and test data strategy. Ensure testability and maintainability."40- Output: Test architecture, fixture design, mock strategy41- Validation: Architecture supports isolated, fast, reliable tests4243## Phase 2: RED - Write Failing Tests4445### 3. Write Unit Tests (Failing)46- Use Task tool with subagent_type="general-purpose"47- Prompt: "Write FAILING unit tests for: $ARGUMENTS. Tests must fail initially. Include edge cases, error scenarios, and happy paths. DO NOT implement production code."48- Output: Failing unit tests, test documentation49- **CRITICAL**: Verify all tests fail with expected error messages5051### 4. Verify Test Failure52- Use Task tool with subagent_type="general-purpose"53- 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."54- Output: Test failure verification report55- **GATE**: Do not proceed until all tests fail appropriately5657## Phase 3: GREEN - Make Tests Pass5859### 5. Minimal Implementation60- Use Task tool with subagent_type="general-purpose"61- 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."62- Output: Minimal working implementation63- Constraint: No code beyond what's needed to pass tests6465### 6. Verify Test Success66- Use Task tool with subagent_type="general-purpose"67- Prompt: "Run all tests for: $ARGUMENTS and verify they pass. Check test coverage metrics. Ensure no tests were accidentally broken."68- Output: Test execution report, coverage metrics69- **GATE**: All tests must pass before proceeding7071## Phase 4: REFACTOR - Improve Code Quality7273### 7. Code Refactoring74- Use Task tool with subagent_type="general-purpose"75- Prompt: "Refactor implementation for: $ARGUMENTS while keeping tests green. Apply SOLID principles, remove duplication, improve naming, and optimize performance. Run tests after each refactoring."76- Output: Refactored code, refactoring report77- Constraint: Tests must remain green throughout7879### 8. Test Refactoring80- Use Task tool with subagent_type="general-purpose"81- Prompt: "Refactor tests for: $ARGUMENTS. Remove test duplication, improve test names, extract common fixtures, and enhance test readability. Ensure tests still provide same coverage."82- Output: Refactored tests, improved test structure83- Validation: Coverage metrics unchanged or improved8485## Phase 5: Integration and System Tests8687### 9. Write Integration Tests (Failing First)88- Use Task tool with subagent_type="general-purpose"89- Prompt: "Write FAILING integration tests for: $ARGUMENTS. Test component interactions, API contracts, and data flow. Tests must fail initially."90- Output: Failing integration tests91- Validation: Tests fail due to missing integration logic9293### 10. Implement Integration94- Use Task tool with subagent_type="general-purpose"95- Prompt: "Implement integration code for: $ARGUMENTS to make integration tests pass. Focus on component interaction and data flow."96- Output: Integration implementation97- Validation: All integration tests pass9899## Phase 6: Continuous Improvement Cycle100101### 11. Performance and Edge Case Tests102- Use Task tool with subagent_type="general-purpose"103- Prompt: "Add performance tests and additional edge case tests for: $ARGUMENTS. Include stress tests, boundary tests, and error recovery tests."104- Output: Extended test suite105- Metric: Increased test coverage and scenario coverage106107### 12. Final Code Review108- Use Task tool with subagent_type="Plan"109- Prompt: "Perform comprehensive review of: $ARGUMENTS. Verify TDD process was followed, check code quality, test quality, and coverage. Suggest improvements."110- Output: Review report, improvement suggestions111- Action: Implement critical suggestions while maintaining green tests112113## Incremental Development Mode114115For test-by-test development:1161. Write ONE failing test1172. Make ONLY that test pass1183. Refactor if needed1194. Repeat for next test120121Use this approach by adding `--incremental` flag to focus on one test at a time.122123## Test Suite Mode124125For comprehensive test suite development:1261. Write ALL tests for a feature/module (failing)1272. Implement code to pass ALL tests1283. Refactor entire module1294. Add integration tests130131Use this approach by adding `--suite` flag for batch test development.132133## Validation Checkpoints134135### RED Phase Validation136- [ ] All tests written before implementation137- [ ] All tests fail with meaningful error messages138- [ ] Test failures are due to missing implementation139- [ ] No test passes accidentally140141### GREEN Phase Validation142- [ ] All tests pass143- [ ] No extra code beyond test requirements144- [ ] Coverage meets minimum thresholds145- [ ] No test was modified to make it pass146147### REFACTOR Phase Validation148- [ ] All tests still pass after refactoring149- [ ] Code complexity reduced150- [ ] Duplication eliminated151- [ ] Performance improved or maintained152- [ ] Test readability improved153154## Coverage Reports155156Generate coverage reports after each phase:157- Line coverage158- Branch coverage159- Function coverage160- Statement coverage161162## Failure Recovery163164If TDD discipline is broken:1651. **STOP** immediately1662. Identify which phase was violated1673. Rollback to last valid state1684. Resume from correct phase1695. Document lesson learned170171## TDD Metrics Tracking172173Track and report:174- Time in each phase (Red/Green/Refactor)175- Number of test-implementation cycles176- Coverage progression177- Refactoring frequency178- Defect escape rate179180## Anti-Patterns to Avoid181182- Writing implementation before tests183- Writing tests that already pass184- Skipping the refactor phase185- Writing multiple features without tests186- Modifying tests to make them pass187- Ignoring failing tests188- Writing tests after implementation189190## Success Criteria191192- 100% of code written test-first193- All tests pass continuously194- Coverage exceeds thresholds195- Code complexity within limits196- Zero defects in covered code197- Clear test documentation198- Fast test execution (< 5 seconds for unit tests)199200## Notes201202- Enforce strict RED-GREEN-REFACTOR discipline203- Each phase must be completed before moving to next204- Tests are the specification205- If a test is hard to write, the design needs improvement206- Refactoring is NOT optional207- Keep test execution fast208- Tests should be independent and isolated209210TDD implementation for: $ARGUMENTS