Without tests, Claude’s only way to verify its work is its own judgment, which degrades as the context window fills up. Tests create an external oracle that stays accurate regardless of session length.
The Red-Green-Refactor Loop
Step 1: "Write a failing test for user registration that validates email format,
password strength, and duplicate detection. Do NOT write the implementation."
Step 2: "Run the tests and confirm they fail."
Step 3: "Now write the minimum implementation to make all tests pass."
Step 4: "Run the tests. If green, refactor the implementation for clarity."
Why This Works Better Than Implementation-First
Claude defaults to writing the “happy path” and ignoring edge cases. When you write tests first that cover edge cases, Claude is forced to handle them. The test failures provide unambiguous feedback, no guessing whether the code is correct.
Add TDD to Your CLAUDE.md
## Development Process
- ALWAYS write failing tests before implementation
- Use AAA pattern: Arrange-Act-Assert
- Test names should describe behavior: "should reject duplicate emails"
- Run tests after every change
- Never modify tests to make them pass. Fix the implementation
Pro Tip: Vertical Slice Testing
Use “tracer bullet” tests that exercise a feature end-to-end (API route → service → database). This prevents Claude from faking implementations since the integration test will catch disconnects between layers.