| 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
Red-Green-Refactor
RED — Write Failing Test
- One behavior per test
- Clear name describing behavior
- Real code (no mocks unless unavoidable)
- Tests what SHOULD happen
Verify RED — Watch It Fail (MANDATORY. Never skip.)
pytest tests/path/test.py -v
Confirm: Test fails (not errors), failure message is expected, fails because feature missing.
Test passes? You're testing existing behavior. Fix test.
GREEN — Minimal Code
Write simplest code to pass the test. Don't add features, refactor other code, or "improve" beyond the test.
Verify GREEN — Watch It Pass (MANDATORY.)
Confirm: Test passes, other tests still pass, output pristine.
REFACTOR — Clean Up
After green only: Remove duplication, improve names, extract helpers. Keep tests green. Don't add behavior.
Repeat
Next failing test for next feature.
Common Rationalizations
| 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. |
| "TDD is dogmatic, I'm being pragmatic" | TDD IS pragmatic. Debugging in production is slower. |
Red Flags — STOP and Start Over
- Code before test
- Test after implementation
- Test passes immediately
- Can't explain why test failed
- "I already manually tested it"
- "Tests after achieve the same purpose"
- "It's about spirit not ritual"
- "Keep as reference" or "adapt existing code"
- "TDD is dogmatic, I'm being pragmatic"
All of these mean: Delete code. Start over with TDD.
Verification Checklist
Before marking work complete:
Final Rule
Production code → test exists and failed first
Otherwise → not TDD
No exceptions without your human partner's permission.