Make a coding agent write the failing tests first, then implement
Split the work into separate prompts: tests only, confirm they fail for the right reason, commit, then implement without touching the tests.

Tools used
- Claude CodeWrites the tests, then the implementation
- GitCommits the failing tests as a baseline
Step navigator
Committed failing tests, an implementation that makes them pass, and test files the agent did not edit.
Developers using Claude Code, Codex or Cursor on code with a test runner.
Before you start
Developers on r/ClaudeCode who work test-first with Claude Code agree: let the agent write tests and code in one go and it writes the code first, then back-fills tests that fit. The fix is separate prompts, and checking the red step yourself.
Step-by-step instructions
Prompt for tests only
First prompt: tests for the behaviour, no implementation. One developer: "first prompt is write the failing tests for X. dont implement." Name the cases; Anthropic's guide contrasts "add tests for foo.py" with naming the edge case and saying to avoid mocks.
Write tests for the proration function in test/billing/: mid-cycle upgrade, mid-cycle downgrade, zero-length cycle, cancelled subscription. Follow the existing test style. Do not write the implementation. Run them and show me the failing output.

Why this works
Separate prompts stop the agent writing code first and tests to fit.
Read the tests and confirm they fail for the right reason
"run them and show me theyre red. (this is the step everyone skips, and claude WILL happily claim tests pass without running them if you let it.)" Another developer reads the tests and confirms "they fail for the right reason", not because of a typo or import error.
Commit the failing tests
Commit them so any later edit to the test files shows up in git diff. An r/ClaudeAI user's loop starts the same way: "write failing tests first, git commit."
git add test/ && git commit -m "Add failing tests for proration"
Implement in a fresh prompt and forbid test edits
Second prompt: make the tests pass and don't modify the test file; if a test looks wrong, report why instead. Some clear the session first so the implementer meets the tests as a given.
Implement calculateProration so every test in test/billing passes. Do not modify any file under test/. If you think a test is wrong, explain why without changing it. Run the tests and paste the result.
Make the rule stick
Add a line to CLAUDE.md: "never write source code in the same turn as new tests." Some developers also use a hook that blocks source edits when there's no matching test file.
End of guide · Reviewed Oct 26, 2026
Pro tips2
- Ask for boundary cases explicitly; agents tend to test representative values only.
- Check git diff on the test folder after implementation.
Common mistakes3
- One prompt that says "write tests and implement it".
- Not reading the failing output yourself.
- Letting the agent edit tests to make them pass.


