Omniscio

OmniscioGuidesPromptsWriting tests

Claude Code prompt for writing tests

A generated test is only worth having if it would fail when the code is wrong and keep passing when the code is reorganised. Which of those two you get is decided by what the prompt asks for.

The prompt below asks for the contract rather than the implementation, so the tests it writes survive the next refactor and fail when the behaviour breaks.

Checked against Anthropic's Claude Code documentation on November 25, 2026. By the Omniscio team. Updated .

The prompt

Write tests for src/notifications/service.ts. Test the behaviour a caller depends on, not the internals: for each exported function, the inputs a real caller sends, the edge cases (empty, missing, too large), and what the caller sees when the downstream call fails.

Match the style of the tests already in src/notifications/__tests__/ — the same runner, the same assertion helpers, the same file layout.

Run the new tests and show me the command and its output. Every test must pass without changing production code to make it pass.

If a test exposes a real bug, stop and tell me before you touch the test.

Both paths are yours to set. They are doing different work — one says what to test, the other says what to look like — so keep both kinds of reference. If the project has no tests yet, say which runner you use instead of pointing at a folder.

Why each line is there

"Write tests for src/notifications/service.ts"

One file, not the project. A test prompt aimed at a module produces tests that share the module's assumptions; one aimed at "the codebase" produces thin tests everywhere and no complete set anywhere. If you want wider coverage, run the prompt once per file, so each run has a subject it can finish.

"Test the behaviour a caller depends on, not the internals"

This is the difference between a test and a mirror. A test of behaviour fails when the contract breaks and survives a refactor; a test of internals fails when the code is reorganised and passes when the contract is broken. Since a refactor is exactly what you will do to this file next, the choice is not academic.

"for each exported function, the inputs a real caller sends"

Anthropic's guidance is to be specific about what behaviour you want to verify, because Claude examines your existing test files to match the style, frameworks and assertion patterns already in use. Naming the surface — exported functions — and the source of the inputs — real callers — narrows it to what someone will actually do with the code.

"the edge cases (empty, missing, too large)"

Anthropic's own suggestion for improving coverage is to ask for the edge cases you might have missed, because error conditions and boundary values are the ones that are often overlooked. The prompt here names three so the shape is set from the first test: without that, a file that opens with it('works') and stops is a common result.

"Match the style of the tests already in …"

Pointing at an existing file is one of the patterns Anthropic names, and for tests it does double duty: the new file reads like the old ones, and Claude has to read a working example of your runner and your assertion library rather than infer them. A test file that uses a different helper than every other test file costs more to maintain than it saves.

"Run the new tests and show me the command and its output"

The command and the output are the evidence. All tests pass on its own tells you very little, and re-running the check to confirm it costs more than reading two lines. Anthropic's advice is to ask for exactly this, every time.

"without changing production code to make it pass"

Claude stops when the work looks done, and the cheapest way to make a failing test look done is to change the code under it. That is sometimes right — a test can catch a real interface mismatch — but it is a decision, not a step, and this line makes it visible. Without it, a passing suite can mean the tests were written to match what the code does rather than what it should do.

"If a test exposes a real bug, stop and tell me"

Same reason, one level up. A test that fails against untouched code has two possible causes with opposite fixes: the expectation is wrong, or the code is. Asking Claude to name which one it found, before touching either, is the whole discipline of writing a test first.

The subagent version

Running a test suite is one of the cases Anthropic lists for a subagent: it keeps long output, like a full test run, out of your main conversation, where it would otherwise ride along for the rest of the session. The run's output stays in the subagent, and you get the summary.

Use a subagent for this job, and have it report back only what I ask for.

Write tests for src/notifications/service.ts. Test the behaviour a caller depends
on, not the internals. Match the style of the tests already in
src/notifications/__tests__/. Run the new tests. Every test must pass without
changing production code to make it pass. If a test exposes a real bug, stop and
tell me before you touch the test.

It should report back only: the file it created, the command it ran, the pass and
fail counts, and the full text of any failure. Not the passing output.

The reporting paragraph is the part people omit, and it is the reason to use a subagent at all. Anthropic's list of ways to spend less on subagents includes asking for short summaries back, so that twenty results do not fill your context.

Once the tests exist, the site also has a ready-made agent for running them: the test runner subagent, and four more files in the subagent examples.

When the plain prompt is better

If the test file is new and short, ask in place. A subagent has to gather the context your conversation already has, so on a small job it can be slower as well as more expensive. Use a subagent when the output you would have to read is longer than the output you want.

What good output looks like

  • One test file, in the right place, matching the neighbours. Same runner, same helpers, same naming.
  • Test names that read as sentences about behaviour. "rejects an empty recipient list" is a contract; "calls notifier.send once" is a description of the implementation.
  • The command and the counts. Not a claim that it passed.
  • A case beyond the three you named, or a line saying those three cover it — which is the honest answer for a simple function.
  • A list of anything it found. If the code has a bug, that list is the most valuable part of the answer.

The common failure

The suite goes green and proves nothing. It usually looks like one of these:

  • The mirror test. Every test asserts that a method was called, or restates the implementation line by line. Break the contract and the test still passes; rename a private function and dozens of tests fail.
  • The bent test. A test fails against the real code, and the expectation is adjusted until it passes. The suite is green, the bug is still there, and the test now protects it.
  • The happy path only. One case per function, all of them the obvious one. Coverage looks respectable and the error paths are untested.

All three are prevented by what the prompt asks for, one instruction each: name the subject as behaviour, name the edge cases, forbid production changes that exist only to make a test pass, and stop when a test exposes a bug. That is what the prompt on this page spends its length on.

There is a fourth, subtler one: tests that pass because they never actually run. A skipped test, a suite that matched no files, a runner invoked with the wrong config. The command and its output catch this where a summary does not, which is why the prompt asks for both.

Make it a one-word command

Save the prompt as a skill and it becomes /test-that: a SKILL.md at .claude/skills/test-that/SKILL.md, with YAML frontmatter between two lines of three dashes, whose body loads only when it is used. The two things worth putting in it beyond the prompt are your test commands — unit, integration, and how to run one file — and the naming convention your suite uses.

The test commands also belong in CLAUDE.md, because they are true of every task rather than only this one. Anthropic calls the command that runs your check the single most useful line in the file, and it is the line that turns it passed into here is the output without you asking.

Both patterns have worked examples here: skills examples and CLAUDE.md examples. If what you want is the whole job rather than the prompt, the everyday tasks guide walks it through step by step.

How this page was written

Everything here about what Anthropic recommends when asking for tests, how it suggests finding edge cases, what evidence to ask for, and what a subagent costs was read from Anthropic's Claude Code documentation on November 25, 2026, and checked against the wording in the Sources below.

The prompt is ours: an example written for this site, following the patterns Anthropic documents. It is not a prompt Anthropic publishes, and the paths in it are placeholders. One deliberate departure: Anthropic's test recipe ends by running the new tests and fixing any failures, while this prompt stops and reports when a test exposes a real bug, because a failing test against untouched code has two possible causes and they need opposite fixes.

Sources

  • Anthropic's Common workflows, for the test recipe, matching existing patterns, and asking for edge cases, read November 25, 2026.
  • Anthropic's Best practices for Claude Code, for the check that returns pass or fail, asking for evidence, what subagents cost, and the CLAUDE.md command line, read November 25, 2026.
  • Anthropic's Prompt library, for pointing at a reference as one of the patterns behind a working prompt, read November 25, 2026.
  • Anthropic's Extend Claude with skills, for where a skill lives and when its body loads, read November 25, 2026.
  • Google's autocomplete suggestions for "claude code test prompt", "claude code prompt for tests" and "claude code prompt for writing tests", read November 25, 2026, for the queries this page answers.

Common questions

What is a good Claude Code prompt for writing tests?

One that asks for the behaviour a caller depends on rather than the internals, points at the tests you already have so the style matches, and asks for the command and output that show them passing. The prompt at the top of this page is an example you can copy.

How do I stop Claude writing tests that just mirror the implementation?

Name the subject as behaviour: the inputs a real caller sends and what the caller sees back. A test that asserts an internal helper was called breaks on the next refactor and proves nothing about the contract, and naming the edge cases keeps the attention on inputs rather than on structure.

What should I do when a generated test fails?

Find out which side is wrong before either changes. A failing test against untouched production code is either a wrong expectation or a real bug, and those need opposite fixes. Ask Claude to tell you which, and to stop when it is a bug rather than editing the test to fit.

Should a subagent write my tests?

It is a good fit for running them, and a reasonable one for writing them, because the output is long and the answer is short. Ask for a summary with the failures in full and nothing else, or the subagent's context saving is lost the moment it reports.

Why does the prompt ask for the command as well as the result?

Because a passing suite can mean the wrong suite ran. The command and its output show which tests executed and how many, which is the difference between it passed and these four ran and passed.

Tests in one session. Sessions in one window.

A test-writing prompt runs inside one Claude Code session. Omniscio is the level above: a desktop app for running Claude Code and other coding agents side by side, with one list of sessions and one inbox, so an agent only interrupts you when a decision needs you.

The features page lists what is in it today, pricing covers the plans, and you can download it for Windows, macOS or Linux.