Is Claude Code good for testing?
Yes. Claude Code writes unit and integration tests in your project's existing style, finds untested code, runs your test command and fixes what fails. Tests are also the best tool for steering Claude itself, because a test suite gives it a pass or fail signal it can read on its own.
Anthropic's best practices put testing at the top of the list: "Give Claude a check it can run: tests, a build, a screenshot to compare. It's the difference between a session you watch and one you walk away from." This guide covers both sides: getting Claude to write good tests, and using tests to keep Claude honest.
What testing work can Claude Code do?
- Match your conventions. The common workflows page says Claude "examines your existing test files to match the style, frameworks, and assertion patterns already in use."
- Find coverage gaps. Ask it to list functions with no tests, then add them. It can suggest tests for error conditions, boundary values and unexpected inputs.
- Reproduce bugs. Ask for a failing test that shows the bug before any fix.
- Fix failing tests. Paste the output or reference a log with
@, and ask for the root cause. - Hunt flaky tests. The dynamic workflows page includes a prompt that runs the suite repeatedly, records intermittent failures and stops once two rounds in a row find nothing new.
- Test in a browser or simulator. Claude can drive Chrome, preview your app in the desktop app, and on macOS run iOS apps in the iOS Simulator pane, which is in public beta.
Anthropic also describes its own teams doing this. In How Anthropic teams use Claude Code, the Product Design team "uses Claude Code to write comprehensive tests for new features," and the Security Engineering team moved to "asking Claude for pseudocode, guiding it through test-driven development, and checking in periodically."
How do you set up a project so Claude tests well?
- Put the test commands in CLAUDE.md. Run
/initto generate a starter file, then add how to run one test, one file and the whole suite. The example CLAUDE.md in Anthropic's best practices includes "Prefer running single tests, and not the whole test suite, for performance." See the CLAUDE.md guide. - State the rules for tests. Which framework, where fixtures live, whether mocks are allowed, and that assertions must never be weakened to get a pass.
- Add a language server. A code intelligence plugin lets Claude see type errors its edits introduce before a test run.
- Make sure the suite passes first. A red suite at the start hides whether Claude broke anything.
How do you make Claude run the tests until they pass?
There are three levels, from least to most setup:
- In the prompt. End a request with "run the tests and fix any failures." This works today on any task.
- With
/goal. Set a condition and Claude keeps working across turns until a separate evaluator judges it met. The goal docs use this example:/goal all tests in test/auth pass and the lint step is clean. Add a turn limit such as "or stop after 20 turns" to bound it. - With a Stop hook. A hook can block Claude from ending its turn until a check passes. This example comes from Anthropic's hooks guide, which marks agent hooks as experimental:
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "agent",
"prompt": "Verify that all unit tests pass. Run the test suite and check the results. $ARGUMENTS",
"timeout": 120
}
]
}
]
}
}
For a deterministic gate, use a command hook that runs your test script instead. The hooks guide on this site explains exit codes and the cap on repeated blocks.
How do you do test-driven development with Claude Code?
Ask for the test first and make Claude prove it fails. Anthropic's best practices give this pattern for a bug: "write a failing test that reproduces the issue, then fix it." For new code, give example inputs and expected outputs in the prompt and say "run the tests after implementing."
Two refinements help:
- Split the roles. The best practices page suggests you "have one Claude write tests, then another write code to pass them." A fresh session isn't biased toward code it just wrote.
- Use a TDD skill. The tdd skill from Matt Pocock and the Superpowers test-driven-development skill both enforce red, green, refactor. For broader input coverage, the property-based testing skill from Trail of Bits covers Hypothesis, fast-check and proptest.
How do you test web apps and UIs?
Start Claude Code with claude --chrome after installing the Claude in Chrome extension. Anthropic lists "test form validation, check for visual regressions, or verify user flows" among its uses. Their example prompt:
I just updated the login form validation. Can you open localhost:3000,
try submitting the form with invalid data, and check if the error
messages appear correctly?
Chrome runs in a visible window and shares your browser's login state, and Claude pauses for you on login pages and CAPTCHAs.
Other options:
/verifyand/run. These bundled skills build and launch your app to confirm a change works, "rather than relying on tests or type checks."/run-skill-generatorrecords the launch recipe as a project skill so later runs follow it.- The desktop app's Browser pane. Claude can start a dev server, test endpoints and read server logs. See the desktop guide.
- Playwright skills. The webapp-testing skill, Playwright CLI skill and Playwright best practices skill help with scripted end-to-end tests.
How do you stop Claude from gaming the tests?
An agent asked to make tests pass can take shortcuts: changing expected values, deleting a test, or mocking away the thing under test. Guard against it:
- Say what success means. Anthropic's best practices use the wording "address the root cause, don't suppress the error." Name constraints too, such as "no other test file is modified."
- Ask for evidence. Have Claude show the command it ran and the output, not just a claim of success.
- Require approval for test edits. Here is an example project settings file, written for this guide with the documented permission syntax, that makes Claude ask before editing anything in
tests/:
{
"permissions": {
"ask": ["Edit(/tests/**)"]
}
}
- Review in a fresh context. Run
/code-reviewon the diff, or ask a subagent to flag weakened or deleted assertions. Anthropic warns that a reviewer asked to find gaps usually finds some, so tell it to flag only gaps that affect correctness.
Which prompts work well for testing?
These are example prompts written for this guide.
List the functions in src/payments/ that have no tests. For the three riskiest, write tests that follow the patterns in tests/payments/. Cover error paths and boundary values. Run them and show the output.
Users report that checkout fails when the cart is empty. Write a failing test that reproduces it, run it to confirm it fails, then fix the code. Do not change any existing assertions.
Run the test suite. For each failure, explain the root cause in one line and fix it. If a test itself looks wrong, stop and ask me before changing it.
Use a subagent to review this diff. Flag any test whose expected value changed, any deleted test, and any new mock that replaces the code under test.
The unit test prompts and API testing prompts collections have more.
Can Claude Code test pull requests in CI?
Yes. The Claude Code GitHub Action responds to @claude mentions, so a comment like "@claude fix the failing test in this PR" works once it is set up with /install-github-app. In the desktop app, an open pull request shows its CI checks, and an Auto-fix option has Claude fix failing checks. For your own scripts, claude -p runs one task and exits; see the headless guide.
OpenAI's Codex GitHub Action runs codex exec in CI jobs, covered in the Codex GitHub Action guide. The same rules apply to any agent: a runnable check, protected tests and human review.
How do you keep your testing prompts in one place?
Good test prompts, such as your edge case checklist or a "reproduce first" bug prompt, are worth reusing. Kipt keeps prompts and Agent Skills as Markdown in your own private GitHub repo, with {{name}} blanks for the module or framework, and installs your skills into Claude Code or Codex with npx skills add. The free SKILL.md generator turns a prompt into a skill folder.