RED-GREEN-REFACTOR implementation methodology - write failing test first, minimal implementation to pass, then refactor. Ensures tests verify behavior.
Inherits all available tools
Additional assets for this skill
This skill inherits all available tools. When active, it can use any tool Claude has access to.
name: test-driven-development description: | RED-GREEN-REFACTOR implementation methodology - write failing test first, minimal implementation to pass, then refactor. Ensures tests verify behavior.
trigger: |
skip_when: |
related: complementary: [testing-anti-patterns, verification-before-completion]
compliance_rules:
id: "test_file_exists" description: "Test file must exist before implementation file" check_type: "file_exists" pattern: "**/*.test.{ts,js,go,py}" severity: "blocking" failure_message: "No test file found. Write test first (RED phase)."
id: "test_must_fail_first" description: "Test must produce failure output before implementation" check_type: "command_output_contains" command: "npm test 2>&1 || pytest 2>&1 || go test ./... 2>&1" pattern: "FAIL|Error|failed" severity: "blocking" failure_message: "Test does not fail. Write a failing test first (RED phase)." prerequisites:
name: "test_framework_installed" check: "npm list jest 2>/dev/null || npm list vitest 2>/dev/null || which pytest 2>/dev/null || go list ./... 2>&1 | grep -q testing" failure_message: "No test framework found. Install jest/vitest (JS), pytest (Python), or use Go's built-in testing." severity: "blocking"
name: "can_run_tests" check: "npm test -- --version 2>/dev/null || pytest --version 2>/dev/null || go test -v 2>&1 | grep -q 'testing:'" failure_message: "Cannot run tests. Fix test configuration." severity: "warning" composition: works_well_with:
skill: "systematic-debugging" when: "test reveals unexpected behavior or bug" transition: "Pause TDD at current phase, use systematic-debugging to find root cause, return to TDD after fix"
skill: "verification-before-completion" when: "before marking test suite or feature complete" transition: "Run verification to ensure all tests pass, return to TDD if issues found"
skill: "requesting-code-review" when: "after completing RED-GREEN-REFACTOR cycle for feature" transition: "Request review before merging, address feedback, mark complete"
conflicts_with: []
Write the test first. Watch it fail. Write minimal code to pass.
Core principle: If you didn't watch the test fail, you don't know if it tests the right thing.
Violating the letter of the rules is violating the spirit of the rules.
Always:
Exceptions (ask your human partner):
Thinking "skip TDD just this once"? Stop. That's rationalization.
NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST
Write code before the test? You have ONLY two options:
If you wrote code before test:
rm [files] # or git reset --hard
Not tomorrow. Not after asking. NOW.
Delete means DELETE:
rm -rf the_file.py ✓git reset --hard ✓These are NOT deleting (violations):
git stash - That's hiding, not deletingmv file.py file.py.bak - That's keepingDelete means gone forever. No recovery possible.
Then start over with TDD.
You CANNOT:
If you think you have a legitimate reason to skip deletion:
The rule has ZERO exceptions. ZERO.
Untested code WILL cause production incidents. These are CERTAINTIES:
❌ "This time is different" → Every developer who thought this experienced these consequences.
DELETE THE CODE. NOW.
rm /tmp/auth-feature/src/auth.ts
rm /tmp/auth-feature/src/middleware.ts
rm /tmp/auth-feature/src/utils.ts
# or: git reset --hard
Then:
ZERO exceptions. Anniversary, deadline, manager pressure, sunk cost - NONE of these override deletion.
You CANNOT create alternatives:
The skill doesn't offer choices. It commands: DELETE.
If you create ANY alternative to deletion, you violated the skill.
DELETE IT. NOW. NO ALTERNATIVES.
Cycle: RED (write failing test) → verify fails → GREEN (minimal code) → verify passes → REFACTOR (clean up) → verify still green → repeat
Write one minimal test showing what should happen.
GOOD: test('retries failed operations 3 times', ...) - clear name, tests real behavior, one thing.
BAD: test('retry works', ...) with mocks - vague name, tests mock not code.
Time limit: <5 minutes. Complex mocks → testing wrong thing. Lots of setup → design too complex. Multiple assertions → split tests.
Requirements:
MANDATORY. Never skip.
npm test path/to/test.test.ts
Paste the ACTUAL failure output in your response:
[PASTE EXACT OUTPUT HERE]
[NO OUTPUT = VIOLATION]
If you can't paste output, you didn't run the test.
| Test Type | Must See This Failure | Wrong Failure = Wrong Test |
|---|---|---|
| New feature | NameError: function not defined or AttributeError | Test passing = testing existing behavior |
| Bug fix | Actual wrong output/behavior | Test passing = not testing the bug |
| Refactor | Tests pass before and after | Tests fail after = broke something |
No failure output = didn't run = violation
Confirm:
Test passes? You're testing existing behavior. Fix test.
Test errors? Fix error, re-run until it fails correctly.
Write simplest code to pass the test.
GOOD: Simple loop with try/catch, just enough to pass. BAD: Adding options, backoff, callbacks (YAGNI).
Don't add features, refactor other code, or "improve" beyond the test.
MANDATORY.
npm test path/to/test.test.ts
Confirm:
Test fails? Fix code, not test.
Other tests fail? Fix now.
After green only:
Keep tests green. Don't add behavior.
Next failing test for next feature.
| Quality | Good | Bad |
|---|---|---|
| Minimal | One thing. "and" in name? Split it. | test('validates email and domain and whitespace') |
| Clear | Name describes behavior | test('test1') |
| Shows intent | Demonstrates desired API | Obscures what code should do |
| Argument | Reality |
|---|---|
| "Tests after verify it works" | Tests-after pass immediately → proves nothing. Test-first forces failure → proves test works. |
| "Already manually tested" | Ad-hoc ≠ systematic. No record, can't re-run, easy to forget under pressure. |
| "Deleting X hours is wasteful" | Sunk cost fallacy. Keeping unverified code = tech debt. Delete → rewrite with confidence. |
| "TDD is dogmatic" | TDD IS pragmatic: catches bugs pre-commit, prevents regressions, documents behavior, enables refactoring. |
| "Spirit not ritual" | Tests-after = "what does this do?" Tests-first = "what should this do?" Different questions. |
| Excuse | Reality |
|---|---|
| "Too simple to test" | Simple code breaks. Test takes 30 seconds. |
| "I'll test after" | Tests passing immediately prove nothing. |
| "Tests after achieve same goals" | Tests-after = "what does this do?" Tests-first = "what should this do?" |
| "Already manually tested" | Ad-hoc ≠ systematic. No record, can't re-run. |
| "Deleting X hours is wasteful" | Sunk cost fallacy. Keeping unverified code is technical debt. |
| "Keep as reference, write tests first" | You'll adapt it. That's testing after. Delete means delete. |
| "Need to explore first" | Fine. Throw away exploration, start with TDD. |
| "Test hard = design unclear" | Listen to test. Hard to test = hard to use. |
| "TDD will slow me down" | TDD faster than debugging. Pragmatic = test-first. |
| "Manual test faster" | Manual doesn't prove edge cases. You'll re-test every change. |
| "Existing code has no tests" | You're improving it. Add tests for existing code. |
All of these mean: Delete code. Start over with TDD.
Bug: Empty email accepted → RED: test('rejects empty email', ...) → Verify RED: FAIL: expected 'Email required', got undefined → GREEN: Add if (!data.email?.trim()) return { error: 'Email required' } → Verify GREEN: PASS → REFACTOR: Extract validation if needed.
Before marking work complete:
Can't check all boxes? You skipped TDD. Start over.
| Problem | Solution |
|---|---|
| Don't know how to test | Write wished-for API. Write assertion first. Ask your human partner. |
| Test too complicated | Design too complicated. Simplify interface. |
| Must mock everything | Code too coupled. Use dependency injection. |
| Test setup huge | Extract helpers. Still complex? Simplify design. |
Bug found? Write failing test reproducing it. Follow TDD cycle. Test proves fix and prevents regression.
Never fix bugs without a test.
This skill uses these universal patterns:
skills/shared-patterns/state-tracking.mdskills/shared-patterns/failure-recovery.mdskills/shared-patterns/exit-criteria.mdskills/shared-patterns/todowrite-integration.mdApply ALL patterns when using this skill.
| Violation | Detection | Recovery |
|---|---|---|
| Code before test | Implementation exists, no test file | DELETE code (rm), write test, verify RED, reimplement |
| FALSE GREEN | Test passes immediately, no implementation | Test is broken - make stricter until it fails correctly |
| Kept "reference" | Stash/backup/clipboard exists | Delete permanently (git stash drop, rm), start fresh |
Why recovery matters: Test must fail first to prove it tests something. Keeping code means you'll adapt it instead of implementing from tests.
Production code → test exists and failed first
Otherwise → not TDD
No exceptions without your human partner's permission.