tdd
Test-Driven Development (TDD) workflow using Red-Green-Refactor cycle. Write failing tests first, then implement minimal code to pass, then refactor. Triggers: "/tdd", "tdd", "test driven", "red green refactor".
SKILL.md
| Name | tdd |
| Description | Test-Driven Development (TDD) workflow using Red-Green-Refactor cycle. Write failing tests first, then implement minimal code to pass, then refactor. Triggers: "/tdd", "tdd", "test driven", "red green refactor". |
name: tdd description: | Test-Driven Development (TDD) workflow using Red-Green-Refactor cycle. Write failing tests first, then implement minimal code to pass, then refactor. Triggers: "/tdd", "tdd", "test driven", "red green refactor". metadata: short-description: TDD Red-Green-Refactor cycle
TDD Skill - Test-Driven Development
Implement features using the Red-Green-Refactor cycle with Codex consultation.
TDD Cycle
Red: Write failing test
↓
Green: Write minimal code to pass
↓
Refactor: Clean up (tests still pass)
↓
Repeat for next test case
Workflow
Phase 1: Test Design
-
Confirm Requirements
- Input/output specifications
- Edge cases and error conditions
- Acceptance criteria
-
List Test Cases
## Test Cases for {feature} ### Happy Path - [ ] test_basic_case: Basic functionality - [ ] test_typical_input: Common usage pattern ### Edge Cases - [ ] test_empty_input: Handle empty data - [ ] test_boundary_values: Min/max limits ### Error Cases - [ ] test_invalid_input: Reject bad data - [ ] test_missing_required: Handle missing fields -
Consult Codex for Test Strategy (optional)
Task(subagent_type="general-purpose", prompt=""" Consult Codex for TDD test design: codex exec --model o3 --sandbox read-only --full-auto " Design test cases for: {feature} Requirements: {requirements} Suggest: 1. Essential test cases (happy path) 2. Edge cases to consider 3. Error scenarios 4. Recommended test order " 2>/dev/null Return concise test case list. """)
Phase 2: Red-Green-Refactor
Step 1: Red - Write Failing Test
# tests/test_{module}.py
import pytest
from src.{module} import {function}
def test_{function}_basic():
"""Test the most basic case."""
result = {function}(input_data)
assert result == expected_output
Run and confirm failure:
uv run pytest tests/test_{module}.py -v
Step 2: Green - Minimal Implementation
Write the minimum code to make the test pass:
# src/{module}.py
def {function}(input_data):
"""Implement {feature}."""
# Minimal implementation - can hardcode initially
return expected_output
Run and confirm pass:
uv run pytest tests/test_{module}.py -v
Step 3: Refactor
Improve code quality while keeping tests green:
- Remove duplication
- Improve naming
- Extract functions if needed
- Add type hints
# Verify tests still pass after refactor
uv run pytest tests/test_{module}.py -v
Step 4: Next Test Case
Return to Step 1 with the next test case from your list.
Phase 3: Completion Check
-
Run Full Test Suite
uv run pytest tests/ -v -
Check Coverage
uv run pytest tests/ --cov=src --cov-report=term-missing -
Target: 80%+ coverage
-
Quality Checks
poe quality
Report Format
After completing TDD cycle, document results:
## TDD Report: {feature}
### Test Cases
- [x] test_basic_case - PASS
- [x] test_edge_case - PASS
- [x] test_error_handling - PASS
### Coverage
- Lines: 85%
- Branches: 78%
### Files Created/Modified
- `src/{module}.py` - Main implementation
- `tests/test_{module}.py` - Test suite
### Notes
- {Any important observations}
Key Principles
- Write tests FIRST - Not after implementation
- Keep cycles small - One test at a time
- Refactor after green - Never refactor red tests
- Minimal code - Only what's needed to pass
- Hardcoding is OK - Improve in refactor phase
When to Use TDD
- New feature implementation
- Bug fixes (write failing test first)
- Refactoring (ensure tests exist first)
- API development
Integration with Codex
For complex implementations, consult Codex during Green phase:
Task(subagent_type="general-purpose", prompt="""
Consult Codex for TDD implementation:
codex exec --model o3 --sandbox read-only --full-auto "
I have a failing test:
{test code}
Error: {error message}
What's the minimal implementation to make this pass?
" 2>/dev/null
Return implementation suggestion.
""")
Example Session
User: "/tdd implement calculate_recommendation_score"
- Design: List test cases for scoring function
- Red: Write
test_score_basic_input - Green: Implement minimal scoring logic
- Refactor: Clean up, add types
- Repeat: Next test case until complete
- Report: Document coverage and results