How Do You Get Claude to Write a Test That Catches the Bug?
Ask for a failing test before the fix, and make Claude show you the failure. If the test doesn't fail on the current code, the theory about the bug is wrong. Anthropic's own sample prompt for a login bug follows this pattern: write a failing test that reproduces it, then fix it.
Bug: [what happens, what should happen, and how to reproduce it]. Start looking in [folder or file]. The test command is `[command for one test file]`. Step 1. Write one failing test at the lowest level that reproduces this. Run it and show me the output. Explain in one or two sentences why it fails. Step 2. Stop and wait for me. Step 3. Once I approve, make the smallest change to the source that turns it green. Don't edit the test. Run the test and the rest of its test file, and show the output. If the test passes on its first run, your theory of the bug is wrong. Tell me, and don't go on to a fix.
- 1Bug report
- 2Failing test, output shown
- 3You approve
- 4Smallest fix
- 5Same test passes
Try it now
- 1Paste the prompt with a real bug from your tracker.
- 2Read the failure in the output. It should describe your bug, such as expected 9 but got 10. A setup error, such as a failed import, proves nothing yet.
- 3Reply
go ahead, then run/diffand check that the fix changed source files and not the test. - 4Ask
temporarily revert the fix, run the test, show that it fails, then restore the fix. A test that goes red again is doing its job.
Use it when
Any non-trivial bug in code with a test harness: wrong output, a boundary condition, or state that breaks after a particular sequence. It's especially useful for a user report you can't yet reproduce by hand.
Skip it when
A throwaway script, or a repo with no harness, where setting one up costs more than the fix is worth. Timing bugs can be hard to pin in a unit test, so add logging first. For a problem that only shows when the app runs, /verify builds and runs it and looks at the result.
Why it works
The failing run is the evidence. It shows Claude has found the condition that causes the bug, and it gives Claude a pass or fail signal to work against. Anthropic's docs say Claude stops when the work looks done, so a check that flips from fail to pass is what tells it the job is finished. The test then stays behind as a guard against the same bug returning. Order matters. Rule of thumb, not a vendor claim: a test written after the fix gets shaped by the fix, and may pass whether or not it catches the original failure. For a bug in code you didn't write, or a flaky test, the docs suggest /effort high or /effort xhigh, so Claude verifies more of its work.
Example
Say a cart total is wrong when a 10% code applies to a single item. The test might be expect(total(cartWith(oneItem, code10off))).toBe(9). Suppose the run reports that it received 10. That failure matches the report, so you approve, and Claude changes where the discount is applied. If the run had failed with a TypeError in the test's setup instead, it wouldn't have reproduced anything, and you'd send it back before any fix.
Going further
For a run you aren't watching, put the constraint in a goal: /goal the test in [file] passes, [test command] exits 0, no test file is modified, and git diff --stat is shown at the end. The goal docs recommend stating what must not change on the way, and the evaluator can only judge what appears in the conversation, which is why the diff stat is in the condition. For a second opinion, /code-review reviews the change, and Anthropic's best-practices page says it runs in a fresh subagent that never saw your conversation.
Common mistakes
- Asking for the fix and the test in the same turn, so the test gets shaped by the fix.
- Accepting a failure for the wrong reason. A bad import or fixture fails too, and says nothing about the bug.
- Letting Claude edit the test during the fix. Say so in the prompt, and check
/diffafterward. - Testing the symptom through the whole app, so the test breaks when a button moves. Ask for the lowest level that still reproduces the bug.
Sources
- Best practices, Claude Code docs
- Keep Claude working toward a goal, Claude Code docs
- Commands, Claude Code docs
Checked 6 November 2026. Products change often, so confirm details with the vendor.