Guide
How to test code written by Claude Code or Cursor
Updated November 17, 2026
Claude Code and Cursor will write tests if you ask, run them if they know how, and keep going until they pass. That is a good start and a trap at the same time. An agent that is graded only by tests it wrote itself will usually find a way to a green result. This guide shows a setup that keeps the speed and adds checks the agent cannot talk its way past.
It works the same way in both tools: tell the agent how to run checks, make the important checks automatic with hooks, review with a fresh pair of eyes, and then confirm that the tests would fail if the code were broken.
Step 1: Give the agent the exact commands
Agents read your repository well, but they cannot guess which test command is the real one, which tests need a database, or how to run a single file. Put that in the instruction file each tool reads: CLAUDE.md for Claude Code, and project rules or AGENTS.md for Cursor. Keep it short and concrete.
## 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.Anthropic's Claude Code guidance puts it plainly: when Claude "said it was done and it wasn't quite," it is usually "because it had no way to check its own work." Give it one.
Step 2: Ask for a failing test first
For bug fixes and behavior changes, ask the agent to write a test that reproduces the problem, run it, and show that it fails before changing any code. Then fix the code and show the same test passing. That sequence gives you evidence that the test is connected to the behavior, not just to the implementation the agent happened to write.
A prompt that works in either tool:
Users report that refunds over $1,000 are rejected. Write a failing test insrc/billing/refunds.test.tsthat reproduces it, run it and show me the failure, then fixrefunds.tsand show the test passing. Paste the commands and output.
Asking for the commands and output matters. "All tests pass" in a summary tells you little. The output tells you what actually ran.
Step 3: Enforce checks with hooks, not reminders
Instructions in CLAUDE.md or rules are suggestions the model usually follows. Hooks are run by the tool every time. Both Claude Code and Cursor support them, and Cursor can load Claude Code hooks for compatibility.
Claude Code. 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" }] }
]
}
}#!/bin/bash
INPUT=$(cat)
[ "$(echo "$INPUT" | jq -r '.stop_hook_active')" = "true" ] && exit 0
pnpm tsc --noEmit && pnpm vitest run --reporter=dot >/tmp/test.log 2>&1 && exit 0
tail -40 /tmp/test.log >&2
exit 2Cursor. Put a hooks.json in .cursor/ at the project root. afterFileEdit runs after the agent edits a file, and stop runs when the agent finishes. Project hooks also run in Cursor cloud agents.
{
"version": 1,
"hooks": {
"afterFileEdit": [{ "command": ".cursor/hooks/format.sh" }],
"stop": [{ "command": ".cursor/hooks/check.sh", "loop_limit": 5 }]
}
}Keep hook checks fast: typecheck, lint and the unit tests near the change. Leave slow suites to CI.
Step 4: Review with something that did not write the code
The session that wrote the code is a poor reviewer of it, because it shares the same reasoning. Two practical options:
- A fresh session or subagent with only the spec and the diff, asked to find bugs, not style issues. Claude Code's own guidance notes that a fresh reviewer helps "because it hasn't seen your reasoning."
- An AI reviewer on the pull request, such as Cursor Bugbot, GitHub Copilot code review, CodeRabbit, Qodo or Greptile. Bugbot runs on each PR update and posts a
Cursor Bugbotcheck; you can make unresolved findings fail that check.
Then a person who owns the area approves the merge. For code touching money, auth, data deletion or retries, that person should read the tests as carefully as the code.
Step 5: Check that the tests would catch a break
Everything so far shows that tests run and pass. None of it shows they would fail if the code were wrong. Agent-written tests have predictable weak spots:
- assertions on "something was returned" (
toBeDefined,toBeTruthy,> 0) instead of the exact value; - mocks that return what the code expects, so the test checks the mock, not the code;
- snapshot tests regenerated along with the change;
- happy-path tests only, with the error and boundary branches executed but never asserted.
The direct check is to break the code on purpose and rerun the tests. Mutation testing tools do this systematically: Stryker for JavaScript and TypeScript, PIT for Java, mutmut for Python. A targeted version is cheaper: for each function the agent changed that really matters, replace its body with a fixed return value in a copy, rerun the tests that call it, and confirm one fails at its own assertion.
Checks go stale after they are written, not only when they are weak on day one. why your checks go stale covers how tests, eval harnesses, LLM judges and AGENTS.md files drift, and how to re-validate them.
Step 6: Run the check from inside the agent with MCP
Because both tools speak MCP, you can hand this step to the agent and still get evidence that does not depend on its judgment. OrangePro is an MIT-licensed local MCP server that does it. Add it to Claude Code (.mcp.json or ~/.claude.json) or Cursor (~/.cursor/mcp.json or Settings, MCP):
{
"mcpServers": {
"orangepro-local": {
"command": "npx",
"args": ["-y", "@orangepro/mcp-server@latest", "mcp"]
}
}
}Then a prompt like:
Useorangepro_start, thenorangepro_find_test_gaps. For the top gap, write a test at the suggested path, run it, then callorangepro_prove_loopand tell me whether it was proven.
OrangePro ranks functions by how likely they are to change, what they can break and how hard a break would be to notice, with fixed weights and no model. The proof step breaks the function in an isolated copy and reruns the test unchanged. The agent cannot mark its own work as proven: the test either passed on the original and failed on the broken copy, or it did not, and the result is written to a ledger in .orangepro/. Your source files are never edited, and local runs do not upload code.
Step 7: Re-check the merged result before release
Each agent session is reviewed and tested on its own branch. What ships is the combination. Before you cut a release, run the same proof on the functions that changed since the last release, on the merged main branch. That is where OrangePro is designed to sit: after merge, before release. It is not a PR or release gate; it gives your team a list of unprotected risky functions and a test-only patch to review.
Summary table
| Step | Claude Code | Cursor | What it proves |
|---|---|---|---|
| Commands the agent can run | CLAUDE.md | Rules or AGENTS.md | The agent knows how to check its work |
| Failing test first | Prompt | Prompt | The test is tied to the behavior |
| Enforced checks | Stop hook, exit 2 | .cursor/hooks.json stop hook | Typecheck and tests ran every time |
| Independent review | Fresh session, PR reviewer | Bugbot, PR reviewer | Someone else read the diff |
| Tests catch a break | Mutation testing or OrangePro via MCP | Same | Tests fail when the code is wrong |
| Before release | Proof on merged changes | Same | What ships is protected |
Frequently asked questions
Often, yes, especially when you give it the test command 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 project hooks.json in .cursor/ with a stop hook that runs your test command, and an afterFileEdit hook for formatting or fast checks. Exit code 2 from a command hook blocks the action, matching Claude Code's behavior.
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. It works in Claude Code, Cursor, VS Code with Copilot, Codex and other MCP clients.
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.
No. Coverage counts lines executed. A test that asserts toBeDefined() can cover every line and catch almost nothing. Break the code on purpose and see what fails. Checks go stale after they are written, not only when they are weak on day one. why your checks go stale covers how tests, eval harnesses, LLM judges and AGENTS.md files drift, and how to re-validate them.
Try it on your code
Add the local MCP server to Claude Code or Cursor and ask for the top unprotected function. Local runs do not upload your source.
npx -y @orangepro/mcp-server@latest start .Built for after merge, before release. It is not a pull request or release gate. Local scans do not upload source.