StepGleam logoStepGleam
Coding guide

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.

~30 minIntermediate 84/100 5 steps Updated Oct 26, 2026
Claude CodeGit
Make a coding agent write the failing tests first, then implement — illustration for this Coding guide

Tools used

  • Claude CodeWrites the tests, then the implementation
  • GitCommits the failing tests as a baseline
Step navigator0/5 done
0 of 5 steps done
What you'll get

Committed failing tests, an implementation that makes them pass, and test files the agent did not edit.

Who this is for

Developers using Claude Code, Codex or Cursor on code with a test runner.

Read this first

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

Step 1 of 5 · Coding

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.

Copy-ready promptClaude Code
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.
Make a coding agent write the failing tests first, then implement — guide thumbnail
Why this works

Separate prompts stop the agent writing code first and tests to fit.

Step 2 of 5 · Coding

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.

Step 3 of 5 · Coding

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."

Copy-ready promptGit
git add test/ && git commit -m "Add failing tests for proration"
Step 4 of 5 · Coding

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.

Copy-ready promptClaude Code
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.
Step 5 of 5 · Coding

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.
FAQ2
Does this work in Cursor or Codex?
The DEV write-up says the same instructions work in each tool's context file.
What if there is no test setup?
Ask the agent to propose one first.