TDD Workflow Skill - Test-Driven Development
Purpose
Implement features using strict TDD methodology: write a failing test first, implement the minimum code to pass, then refactor while keeping tests green.
Trigger
User invokes /tdd <requirement description> or orchestrator delegates TDD tasks.
Algorithm
Phase 1: RED (Write Failing Test)
- ANALYZE the requirement to identify testable behaviors
- DELEGATE test writing to Tester Expert via Task tool:
- Write test(s) that define the expected behavior
- Use existing test patterns/framework found in the project
- Tests MUST be specific, isolated, and deterministic
- RUN tests via Bash to confirm they FAIL (this is expected)
- REPORT RED status with test output
Phase 2: GREEN (Make Tests Pass)
- DELEGATE implementation to Coder agent via Task tool:
- Write the MINIMUM code to make the failing test(s) pass
- No premature optimization or extra features
- Follow existing code patterns in the project
- RUN tests via Bash to confirm they PASS
- REPORT GREEN status with test output
- If tests still fail, iterate steps 5-7 (max 3 attempts)
Phase 3: REFACTOR (Clean Up)
- DELEGATE refactoring to Coder agent via Task tool:
- Remove duplication
- Improve naming and readability
- Extract functions if needed (max 30 lines per function)
- Ensure type hints and docstrings are present
- RUN tests via Bash to confirm they still PASS
- REPORT REFACTOR status with test output
Completion
- PRESENT final summary with all three phases
Output Format at Each Phase
## TDD Cycle: <requirement>
### [RED] Failing Test
- File: <test file path>
- Test: <test function name>
- Result: FAIL (expected)
- Output: <relevant error output>
### [GREEN] Implementation
- File: <implementation file path>
- Changes: <brief description>
- Result: PASS
- Output: <test pass output>
### [REFACTOR] Cleanup
- Changes: <what was refactored>
- Result: PASS (tests still green)
- Output: <test pass output>
Rules
- NEVER skip the RED phase - always start with a failing test
- NEVER write more code than needed to pass the test in GREEN phase
- NEVER refactor if tests are not green
- If tests cannot be run (no test framework), install it first
- Detect test framework from project: pytest, jest, vitest, go test, cargo test, etc.
- Each TDD cycle handles ONE behavior - repeat for multiple behaviors
- Show test output at every phase transition
- If a test is flaky (passes/fails inconsistently), fix the test first
Test Framework Detection
Check in order:
pytest.ini/pyproject.toml [tool.pytest]/conftest.py-> pytestjest.config.*/package.json "jest"-> jestvitest.config.*-> vitestCargo.toml-> cargo testgo.mod-> go testMakefilewith test target -> make test- Fallback: ask user which test command to use
