Guide

How to test code written by Claude Code

Claude Code will write tests if you ask, run them if it knows how, and keep going until they pass. That is useful, and it is also a trap. When the same session writes the code and the tests, a green result often means the tests share the agent's assumptions. This page is the Claude Code setup: give it the real test commands, enforce them with a hook, review the diff in a fresh session, then confirm the tests would fail if the new code were wrong.

The same method works in Cursor, Codex and GitHub Copilot. A combined walkthrough is in How to test code written by Claude Code or Cursor. For the wider release habit, see How to validate AI-written code before release.

Put the test commands in CLAUDE.md

Claude Code reads CLAUDE.md in the project. Anthropic's guidance is direct: when Claude "said it was done and it wasn't quite," it is usually "because it had no way to check its own work." Write the exact commands, not a vague request to "run the tests."

## How to check work in this repo
- Typecheck: pnpm tsc --noEmit
- Unit tests: pnpm vitest run
- One file: pnpm vitest run path/to/file.test.ts
- Integration tests need DATABASE_URL (see .env.example)
- Never edit snapshot files to make a test pass. Ask first.

For a bug fix, ask for a failing test first, the command output, then the fix and the same test passing. "All tests pass" in a summary is not evidence. The output is.

Enforce checks with a Stop hook

CLAUDE.md is a suggestion the model usually follows. A hook is run by Claude Code every time. A Stop hook runs when Claude is about to finish. A command hook that exits with code 2 blocks the stop and sends its message back to Claude, so it keeps working. Check the stop_hook_active field so the hook does not loop forever.

{
  "hooks": {
    "Stop": [
      { "hooks": [{ "type": "command", "command": ".claude/hooks/check.sh" }] }
    ]
  }
}

Keep the hook script short: typecheck and the tests that should gate a session. Leave slow suites for CI or a later after-merge check.

Review in a fresh session

A second look should not share the first session's reasoning. Anthropic notes that a fresh reviewer helps "because it hasn't seen your reasoning." Start a new Claude Code session, or use a pull request reviewer, and ask it to find bugs in the diff against the spec, not style nits.

Prove the tests catch a break

Hooks prove the tests ran. They do not prove the tests would fail if the new behavior were wrong. Coverage has the same gap: a test that only checks toBeDefined() can execute every line and catch almost nothing. Mutation testing, or a tool that breaks changed behavior on purpose, is the check that answers that question.

OrangePro is a local MCP server for this step. It ranks functions with weak test evidence and can show whether a test catches a break. Add it to Claude Code in a project .mcp.json or in ~/.claude.json. A green build still is not proof. OrangePro is the check you run after the agent's tests are green, before you treat the change as protected.

{
  "mcpServers": {
    "orangepro-local": {
      "command": "npx",
      "args": ["-y", "@orangepro/mcp-server@latest", "mcp"]
    }
  }
}

Or print a client-specific snippet with npx -y @orangepro/mcp-server@latest agent --client claude-code. Mapping, ranking and proof stay on your machine. Drafting tests with real assertions uses a model provider you choose, with your own key.

Frequently asked questions

Often, yes, especially when you put the test command in CLAUDE.md and ask for a failing test first. The risk is that its tests share its assumptions. Confirm that they fail when the code is broken before you rely on them.

Add a Stop hook that runs your test command. If the command hook exits with code 2, Claude Code blocks the stop and sends the hook output back so Claude keeps working. Guard against loops with stop_hook_active.

Yes. OrangePro's local MCP server (npx -y @orangepro/mcp-server@latest mcp) ranks functions with missing or weak test evidence and can prove whether a test catches a break. Add it in .mcp.json or ~/.claude.json.

No for mapping, ranking and proof. Drafting tests with real assertions uses a model provider you choose, with your own key, called directly from your machine.

Try it on your code

Add the local MCP server to Claude Code and ask for the top unprotected function. Local runs do not upload your source.

$npx -y @orangepro/mcp-server@latest agent --client claude-code

Built for after merge, before release. It is not a pull request or release gate. Local scans do not upload source.