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
- Throwaway prototypes
- Generated code
- Configuration files
The Iron Law
- 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
Write one minimal test showing what should happen.<Good>
</Good>
<Bad>
</Bad>
Requirements:
- One behavior
- Clear name
- Real code (no mocks unless unavoidable)
Verify RED - Watch It Fail
MANDATORY. Never skip.- Test fails (not errors)
- Failure message is expected
- Fails because feature missing (not typos)
GREEN - Minimal Code
Write simplest code to pass the test.<Good>
</Good>
<Bad>
</Bad>
Don’t add features, refactor other code, or “improve” beyond the test.
Verify GREEN - Watch It Pass
MANDATORY.- Test passes
- Other tests still pass
- Output pristine (no errors, warnings)
REFACTOR - Clean Up
After green only:- Remove duplication
- Improve names
- Extract helpers
Repeat
Next failing test for next feature.Good Tests
Why Order Matters
“I’ll write tests after to verify it works” Tests written after code pass immediately. Passing immediately proves nothing:- Might test wrong thing
- Might test implementation, not behavior
- Might miss edge cases you forgot
- You never saw it catch the bug
- No record of what you tested
- Can’t re-run when code changes
- Easy to forget cases under pressure
- “It worked when I tried it” ≠ comprehensive
- Delete and rewrite with TDD (X more hours, high confidence)
- Keep it and add tests after (30 min, low confidence, likely bugs)
- Finds bugs before commit (faster than debugging after)
- Prevents regressions (tests catch breaks immediately)
- Documents behavior (tests show how to use code)
- Enables refactoring (change freely, tests catch breaks)
Common Rationalizations
Red Flags - STOP and Start Over
- Code before test
- Test after implementation
- Test passes immediately
- Can’t explain why test failed
- Tests added “later”
- Rationalizing “just this once”
- “I already manually tested it”
- “Tests after achieve the same purpose”
- “It’s about spirit not ritual”
- “Keep as reference” or “adapt existing code”
- “Already spent X hours, deleting is wasteful”
- “TDD is dogmatic, I’m being pragmatic”
- “This is different because…”
Example: Bug Fix
Bug: Empty email accepted REDVerification Checklist
Before marking work complete:- Every new function/method has a test
- Watched each test fail before implementing
- Each test failed for expected reason (feature missing, not typo)
- Wrote minimal code to pass each test
- All tests pass
- Output pristine (no errors, warnings)
- Tests use real code (mocks only if unavoidable)
- Edge cases and errors covered
When Stuck
Debugging Integration
Bug found? Write failing test reproducing it. Follow TDD cycle. Test proves fix and prevents regression. Never fix bugs without a test.Testing Anti-Patterns
When adding mocks or test utilities, read @testing-anti-patterns.md to avoid common pitfalls:- Testing mock behavior instead of real behavior
- Adding test-only methods to production classes
- Mocking without understanding dependencies