Guides / Best Claude Code prompts (2026) / Fix a failing test
Claude Code work order · Updated 2026-10-09
Claude Code prompt: fix a failing test without touching the test
Ask Claude Code to "make the tests pass" and sometimes it does exactly that, by editing the test. This work order fixes the code instead. It keeps the test file read-only, makes Claude explain the cause before it changes anything, and ends with a rerun and a file list you can check in five seconds.
The work order
Copy it, fill in the [brackets], and paste it into Claude Code. It also works in Cursor's Agent mode.
A test is failing and I want the code fixed, not the test. Failing test: [test file and test name, or "run the suite and find it"] Run just that test with: [command] What it checks: [one line on the behavior the test expects] Ground rules: - The test file is read-only. Don't edit, skip, or delete the test, and don't loosen its assertions, timeouts, or snapshots. - Don't add mocks, flags, or special cases that only exist to make this test pass. - If you think the test itself is wrong, stop and tell me why with file:line evidence. - Keep the fix inside [allowed paths]. Ask before touching anything else. - No new dependencies. Steps: 1. Run the failing test and show me the part of the output that matters. 2. Read the code under test and explain the cause in two or three sentences. Point to the line where what the code does and what the test expects split apart. 3. Make the smallest change that fixes that cause. 4. Run the test again. Then run [wider command, e.g. all tests for this module] to make sure nothing nearby broke. 5. Run git diff --name-only and confirm no test files are in the list. Report back: the cause, files changed, the before and after test output, and anything you noticed along the way but didn't fix.
Give it the command for the single test. Without one, Claude often runs the whole suite on every attempt, which is slow and buries the one failure you care about.
Variations
[N] tests started failing after [merge, commit, or version bump]. Don't fix anything yet. Run [test command] and group the failures by likely shared cause. For each group, give me the tests in it, one line on the cause, and the file you'd change. Then wait while I pick which group to fix first. Test files stay read-only for this whole session.
Grouping first stops Claude from fixing the same root cause five different ways in five different places.
[test name] passes sometimes and fails sometimes. Run it [10] times with [command] and write down the results. Look for order dependence, shared state between tests, timing and sleeps, real network or clock calls, and random data without a fixed seed. Tell me which one it is, with evidence, before you change anything. If the fix belongs in test setup rather than the assertions, show me the diff and wait for my OK.
[test name] passes on my machine but fails in CI. Here's the CI log: [paste] Compare the two environments: runtime and dependency versions, env vars, timezone, locale, file paths, and whether tests run in parallel. List the differences that could explain the failure, most likely first, and give me one command that reproduces the CI failure locally. No code changes until we can reproduce it.
When to use it
- One test, or a handful, went red and you trust what the test is checking.
- You want a small diff you can review, not a rewrite of the area.
- You suspect the agent would otherwise "fix" things by relaxing an assertion.
- Skip it when the requirement really changed and the test is out of date. Then update the test yourself, or tell Claude exactly which assertion to change and why.
What good output looks like
- It shows the failing output before touching any code.
- The explanation names a specific line, not a vague "there was an issue with the logic".
- The diff is small and every changed file sits outside your test folders.
- The rerun output is pasted in, both for the single test and the wider run.
- There's a short "noticed but didn't fix" list. An empty list on messy code is a little suspicious.
Common mistakes
- Saying "make the tests pass". That goal can be met by editing the test, so sooner or later it will be.
- Leaving out the single-test command, so every attempt runs the full suite.
- Letting the fix spread. If the cause is in a shared helper, have it stop and tell you before it edits code other modules depend on.
- Accepting "this should pass now" without the actual test output.
- Using this on several unrelated failures at once. Run the triage variation first.
Need a different prompt? Describe your coding goal and get ranked prompts with quality, match & confidence scores.
Rank prompts for my goal →More Claude Code work orders
Related prompts in the library
FAQ
How do I stop Claude Code from editing my tests?
Say it plainly in the prompt: the test file is read-only and assertions can't be loosened. Then check git diff --name-only at the end. For a hard guarantee, add the rule to CLAUDE.md and block edits to your test paths with a permission deny rule or a hook in your Claude Code settings.
What if the test really is wrong?
The work order tells Claude to stop and explain why, with file and line references, instead of changing the test. You decide. If you agree, change the test yourself or give Claude a specific instruction like "update the expected date format in this assertion to ISO 8601".
Do I need plan mode for this?
Usually not for a single failing test. The prompt already makes Claude explain the cause before it edits. Use plan mode, or the triage variation, when several tests broke at once and you want to see the whole picture first.
Does this prompt work in Cursor?
Yes. Paste it into Agent mode and @-mention the failing test file. The rules about read-only tests and the final file list work the same way.