Make Claude Code Write Tests First (2026)
Claude Code writes implementation first and tests as an afterthought. For TDD practitioners, this is backwards. Here’s how to enforce test-first development.
The Problem
Given “add email validation to the signup endpoint,” Claude Code:
- Writes the validation logic
- Writes tests that pass against the implementation
- Tests mirror the code rather than driving design
The tests become documentation of what was built, not specifications of what should be built.
Root Cause
Claude Code’s default behavior optimizes for “task completion” — write code that works. TDD’s red-green-refactor cycle adds steps (write failing test, then implement). Without explicit rules, the agent skips the “failing test” phase.
The Fix
## TDD Protocol (Mandatory)
### Ordering
1. Write the test FIRST (it must fail)
2. Run the test — confirm it FAILS
3. Write the MINIMUM implementation to make it pass
4. Run the test — confirm it PASSES
5. Refactor if needed (tests must still pass)
6. Repeat for next test case
### Rules
- NEVER write implementation before a test exists for it
- Each test must fail before its corresponding implementation
- Run tests after EVERY code change (implementation and refactoring)
- Test file must be created BEFORE the implementation file for new features
### Test Structure
- Test names describe expected behavior, not implementation
- BAD: "test_validateEmail_function"
- GOOD: "rejects emails without @ symbol"
CLAUDE.md Rule to Add
## Test-First Requirement
For any code change that adds or modifies behavior:
1. Write/update the test file FIRST
2. Run tests — show me the failure
3. Then write the implementation
4. Run tests — show me the pass
If you catch yourself writing implementation before tests, STOP,
delete the implementation, write the test, then re-implement.
Verification
Add a function that calculates shipping cost based on weight and destination
Non-TDD response: writes calculateShipping(), then writes tests
TDD response: writes calculateShipping.test.ts with 4 failing test cases, runs them (all fail), then implements calculateShipping(), runs again (all pass)
For a more robust TDD setup, consider SuperClaude’s TDD mode which automates the cycle enforcement.
| Related: Claude Code Best Practices | The Claude Code Playbook | Claude Code Hooks Explained |
See Also
- Claude Code Write Path Outside Workspace — Fix (2026)
- Claude Code Tab Completion Setup Guide 2026
- How to Set Up Claude Code in Ghostty Terminal 2026
- Set Up Claude Code in Dev Containers 2026
- Claude Code + WebStorm JetBrains Setup 2026
Frequently Asked Questions
Do I need a paid Anthropic plan to use this?
Claude Code works with any Anthropic API plan, including the free tier. However, the free tier has lower rate limits (requests per minute and tokens per minute) that may slow down multi-step workflows. For professional use, the Build or Scale plan provides higher limits and priority access during peak hours.
How does this affect token usage and cost?
The token cost depends on the size of your prompts and Claude’s responses. Typical development tasks consume 10K-50K tokens per interaction. Using a CLAUDE.md file and skills reduces exploration tokens by 50-80%, which directly lowers costs. Monitor your usage at console.anthropic.com/settings/billing.
Can I customize this for my specific project?
Yes. All Claude Code behavior can be customized through CLAUDE.md (project rules), .claude/settings.json (permissions), and .claude/skills/ (domain knowledge). The most impactful customization is adding your project’s specific patterns, conventions, and common commands to CLAUDE.md so Claude Code follows your standards from the start.
What happens when Claude Code makes a mistake?
Claude Code creates files and edits through standard filesystem operations, so all changes are visible in git diff. If a change is wrong, revert it with git checkout -- <file> for a single file or git stash for all changes. Claude Code does not make irreversible changes unless you explicitly allow destructive commands in settings.json.
Practical Details
When working with Claude Code on this topic, keep these implementation details in mind:
Project Configuration. Your CLAUDE.md should include specific references to how your project handles this area. Include file paths, naming conventions, and any project-specific patterns that differ from defaults. Claude Code reads this file at session start and uses it to guide all operations.
Integration with Existing Tools. Claude Code works alongside your existing development tools rather than replacing them. It respects .gitignore for file visibility, uses your project’s installed dependencies, and follows the build/test scripts defined in package.json (or equivalent). Ensure your toolchain is working correctly before involving Claude Code.
Performance Considerations. For large codebases (10,000+ files), Claude Code’s file scanning can be slow if not properly scoped. Use .claudeignore to exclude generated directories (dist, build, .next, coverage) and dependency directories (node_modules, vendor). This typically reduces scan time by 80-90%.
Version Control Integration. All changes Claude Code makes are regular filesystem operations visible to git. Use git diff after each significant change to review what was modified. For experimental changes, create a branch first with git checkout -b experiment/topic so you can easily discard or keep the results.
Build yours → Create a custom CLAUDE.md with our Generator Tool.
Related Guides
Estimate tokens → Calculate your usage with our Token Estimator.
Try it: Estimate your monthly spend with our Cost Calculator.