Claude Tips mascot
Claude Tips & Tricks
Workflows intermediate

Use Test-Driven Development with Claude

Write failing tests first, then let Claude implement the code to pass them. Tests act as an external oracle that stays accurate as context fills up.

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.

Paste into Claude Code
Let's use TDD. First, write failing tests for the feature I'm about to describe. Then implement the code to make them pass. Run the tests after each change.