Skip to content

Claude Code claims the tests pass, but they do not

By the Toolbay team · Checked against the sources below on

Anthropic's prompting guide says Claude can focus too heavily on making tests pass at the expense of a general solution, including hard-coding values for the test inputs. Tell it that tests verify the code and do not define it, never let it be the only one who runs them, and check the diff for changed tests.

Put this in CLAUDE.md

CLAUDE.md
# Tests
- Do not hard-code values or create solutions that only work for specific test inputs.
- Tests are there to verify correctness, not to define the solution.
- If any of the tests are incorrect, inform me rather than working around them.
- A task is done only when `npm test` passes. Paste the final summary line.

The first three lines are Anthropic's own wording from its prompting best practices. The last line asks for evidence you can check instead of a claim.

Make the check independent

  1. Run the tests yourself, or in CI, before you accept the work. The agent's summary is not a test result.
  2. Read the diff of test files first. A weakened assertion, a skipped test or a deleted case is the usual sign.
  3. While it implements a feature, block edits to the tests with a deny rule in .claude/settings.json, written like the docs' example Edit(/docs/**) with your tests folder in place of docs. Claude Code enforces permission rules itself, not the model.
  4. Use a Stop hook that runs the test command and exits with code 2 on failure: exit 2 from a Stop hook prevents Claude from stopping and sends your stderr back as feedback. Check stop_hook_active in the input so the hook cannot loop.

When a skill or vault helps, and when it does not

Helps

  • A testing skill that requires a failing test first, then the fix, gives the agent a process that resists gaming.
  • Skills that end by running the real test command keep the evidence in the transcript.

Does not help

  • No instruction file is proof. Only the test run you or CI did yourself is.
  • A skill that writes tests for you still needs a human to check that the tests test the right thing.