| name | test-driven-development |
| description | Use when implementing any feature or bugfix, before writing implementation code |
Test-Driven Development (TDD)
Overview
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.
When to Use
Always:
- New features
- Bug fixes
- Refactoring
- Behavior changes
Exceptions (ask your human partner):
- Throwaway prototypes
- Generated code
- Configuration files
Thinking "skip TDD just this once"? Stop. That's rationalization.
The Iron Law
NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST
Write code before the test? Delete it. Start over.
No exceptions:
- Don't keep it as "reference"
- Don't "adapt" it while writing tests
- Don't look at it
- Delete means delete
Implement fresh from tests. Period.
Red-Green-Refactor
RED - Write Failing Test
Write one minimal test showing what should happen. Clear name, tests real behavior, one thing.
Verify RED - Watch It Fail
MANDATORY. Never skip.
Run the test. Confirm:
- Test fails (not errors)
- Failure message is expected
- Fails because feature is missing (not typos)
Test passes? You are testing existing behavior. Fix the test.
Test errors? Fix the error, re-run until it fails correctly.
GREEN - Minimal Code
Write the simplest code to pass the test. Do not add features, refactor other code, or improve beyond the test.
Verify GREEN - Watch It Pass
MANDATORY.
Run the test. Confirm:
- Test passes
- Other tests still pass
- Output is clean (no errors, warnings)
Test fails? Fix code, not the test.
Other tests fail? Fix now.
REFACTOR - Clean Up
After green only:
- Remove duplication
- Improve names
- Extract helpers
Keep tests green. Do not add behavior.
Repeat
Write the next failing test for the next feature.
Good Tests
| 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 |
Rules that keep tests honest:
- Name the production change that would make the test fail — before writing it
- Assert on real behavior, never on mock behavior
- Keep test-only code in test utilities, out of production classes
- Understand a dependency's side effects before mocking it
Common Rationalizations
| Excuse | Reality |
|---|
| "Too simple to test" | Simple code breaks. Test takes 30 seconds. |
| "I'll test after" | Tests written after pass immediately — which proves nothing. |
| "Already manually tested" | Manual testing has no record, no replay, no edge-case coverage. |
| "Need to explore first" | Fine. Throw away the exploration. Start TDD fresh. |
| "Test hard = design unclear" | Listen to the test. Hard to test means hard to use. |
Red Flags - STOP and Start Over
- Code before test
- Test after implementation
- Test passes immediately without implementation
- Can't explain why the test failed
- "Just this once"
All of these mean: Delete code. Start over with TDD.
Verification Checklist
Before marking work complete:
Can't check all boxes? You skipped TDD. Start over.
Final Rule
Production code → test exists and failed first
Otherwise → not TDD
No exceptions without your human partner's permission.