/tdd
Start a Test-Driven Development workflow for a story, enforcing RED→GREEN→REFACTOR phase discipline.
Quick Start
/agileflow:tdd US-0042Initialize TDD workflow for story US-0042 in the RED phase.
Parameters
| Parameter | Required | Description |
|---|---|---|
<US-ID> | Yes | Story ID to start TDD for |
Usage Examples
Start TDD for a story
/agileflow:tdd US-0042Initialize RED phase for story US-0042. Write failing tests first.
Resume existing TDD
/agileflow:tdd US-0042If TDD is already active for US-0042, resumes from current phase instead of restarting.
TDD Phases
Phase 1: RED
🔴 RED PHASE
═════════════════════════
Write FAILING tests first
Rules:
- Write test files ONLY - no implementation code yet
- Tests should cover the acceptance criteria
- Tests MUST fail when run (they test code that doesn't exist)
- Focus on the public API - what should the code DO?
When ready:
- Write your failing tests
- Run
/agileflow:verifyto confirm tests FAIL - Run
/agileflow:tdd-nextto advance to GREEN
Phase 2: GREEN
🟢 GREEN PHASE
═════════════════════════
Write MINIMAL code to pass tests
Rules:
- Write the simplest possible implementation to pass tests
- Resist the urge to over-engineer or optimize
- If a test needs a complex solution, the test may be too broad
- Do NOT modify test files (except removing .skip())
- Run tests frequently during implementation
When ready:
- Implement code to pass the failing tests
- Run
/agileflow:verifyto confirm tests PASS - Run
/agileflow:tdd-nextto advance to REFACTOR
Phase 3: REFACTOR
🔵 REFACTOR PHASE
═════════════════════════
Clean up while keeping tests GREEN
Rules:
- Extract common patterns into helper functions
- Improve naming and readability
- Reduce duplication
- Run tests after every change - any failure means rollback
- Code should be production-quality
When ready:
- Refactor code to be clean and maintainable
- Run
/agileflow:verifyto confirm tests still PASS - Run
/agileflow:tdd-nextto COMPLETE or start new cycle
Phase 4: COMPLETE
✅ COMPLETE
═════════════════════════
All tests pass, code is clean, ready for review
Actions:
- Run code review before committing
- Commit changes with reference to tests
- Or start new RED→GREEN→REFACTOR cycle for more features
How It Works
The TDD workflow enforces strict phase gates:
RED GREEN REFACTOR COMPLETE
─────────────────────────────────────────────────────────────
Write failing → Tests FAIL? → Failing → Green → Clean?
tests (gate check) tests transition ✓ Done
Cannot advance without test_status = "failing"
Cannot advance without test_status = "passing"
Allowed File Types
In each phase, you can modify:
- Test files:
**/*.test.*,**/*.spec.*,**/tests/**,**/fixtures/** - Implementation: Source files (after RED phase)
- Config: tsconfig.json, jest.config.js, etc.
Integration with Commands
Related Commands
/agileflow:tdd-next- Advance to next phase (GREEN/REFACTOR/COMPLETE)/agileflow:verify- Run tests and update test status/agileflow:tests- Set up testing infrastructure/agileflow:review- Code review before commit/agileflow:babysit- Main implementation workflow (can integrate TDD)
Status Tracking
TDD status is stored in docs/09-agents/status.json:
{
"US-0042": {
"title": "Add user authentication",
"status": "in_progress",
"tdd_phase": "red",
"tdd_started_at": "2026-02-25T10:30:00Z",
"tdd_cycles": 1,
"test_status": "failing"
}
}Fields updated:
tdd_phase: Current phase (red, green, refactor, complete)test_status: Test execution result (failing, passing, error)tdd_cycles: Number of RED→GREEN→REFACTOR cycles completed
Common Workflow
Typical TDD Session
-
Start TDD
/agileflow:tdd US-0042 -
RED Phase - Write failing tests
# Create test file: src/auth.test.js # Write tests that verify auth behavior -
Verify Tests Fail
/agileflow:verify -
Advance to GREEN
/agileflow:tdd-next -
GREEN Phase - Implement
# Create src/auth.js with minimal implementation # Focus only on passing the tests -
Verify Tests Pass
/agileflow:verify -
Advance to REFACTOR
/agileflow:tdd-next -
REFACTOR Phase - Clean up
# Extract helpers, improve naming, reduce duplication # Keep tests passing at all times -
Verify and Complete
/agileflow:verify /agileflow:tdd-next -
Code Review
/agileflow:review
Error Handling
Story Not Found
❌ Story US-0099 not found in status.json
Available stories:
- US-0042: Add user authentication (ready)
- US-0043: Implement settings page (in_progress)
Already in TDD
🔴 Resuming TDD for US-0042 (RED phase, cycle 1)
You're already in TDD mode. Current phase: RED
Write failing tests, then run /agileflow:tdd-next to advance.
No Test Framework
⚠️ No test framework detected for this project.
Run /agileflow:tests first to set up testing infrastructure,
then restart TDD with /agileflow:tdd US-0042.
Best Practices
- Write focused tests - One thing per test
- Test behavior, not implementation - Tests should verify what the code does, not how
- Keep tests simple - If you struggle to write a test, the requirement may be unclear
- Refactor tests too - Test code should be as clean as production code
- Commit after each phase - Creates good commit history of progress
- Don't skip phases - The discipline is the benefit
When TDD Helps Most
- Complex algorithms - Tests help you think through edge cases
- Critical features - High reliability requirements
- Maintainability - Good test suite = confident refactoring
- Team coordination - Tests document expected behavior
- Regression prevention - Tests catch breaking changes
When TDD is Less Applicable
- UI implementation - Hard to test, better with manual verification
- Exploratory code - Spike/POC work may have different process
- Configuration - Hard to test, easier to verify manually
Related Commands
On This Page
/tddQuick StartParametersUsage ExamplesStart TDD for a storyResume existing TDDTDD PhasesPhase 1: REDPhase 2: GREENPhase 3: REFACTORPhase 4: COMPLETEHow It WorksAllowed File TypesIntegration with CommandsRelated CommandsStatus TrackingCommon WorkflowTypical TDD SessionError HandlingStory Not FoundAlready in TDDNo Test FrameworkBest PracticesWhen TDD Helps MostWhen TDD is Less ApplicableRelated Commands