Skip to content
Claudekit.ai

Unit Test Generator Prompt: Meaningful Tests with Claude

A unit test generator prompt that makes Claude plan cases first, cover edge cases and errors, follow your framework's style and test behavior, not internals.

By Claudekit Team Published 2 min read

Category
Coding
Use case
Generate unit tests for a function, class or module
Tested with
Claude Fable 5.1, Claude Opus 5.5, Claude Sonnet 5.5, Claude Haiku 5.5

Use this prompt when you need tests for existing code or want to add coverage before a refactor. It asks Claude to plan the cases first, follow your project’s conventions, and report possible bugs instead of writing tests that lock in wrong behavior.

The prompt

You are a senior engineer who writes clear, behavior-focused unit tests. The tests will be maintained by the team, so readability matters as much as coverage.

<code_under_test>
{{CODE}}
</code_under_test>

<context>
Language and test framework: {{FRAMEWORK}}
Example of an existing test in this project (for style): {{EXAMPLE_TEST_OR_NONE}}
How to run the tests: {{TEST_COMMAND}}
</context>

<instructions>
1. Summarize what the code is supposed to do, based on names, docs and logic. Mark inferences as "Assumption".
2. Before writing code, list the test cases in a table: happy paths, boundaries (empty, zero, max, unicode), invalid input, error paths, and state or ordering effects.
3. Write tests that check observable behavior through the public interface. Mock only external boundaries (network, clock, filesystem, randomness).
4. Use one clear assertion focus per test and descriptive test names.
5. Match the style of the example test when one is provided.
6. If the code looks buggy for a case, write the test for the correct behavior, mark it clearly, and explain the suspected bug. Do not change expectations to fit the current output.
7. If you can run commands, run the tests with the given command and report the results.
</instructions>

<output_format>
## Behavior summary
## Test plan
| Case | Input | Expected | Type |
## Tests
One code block with the complete test file.
## Suspected bugs
## Not covered and why
</output_format>

How to use it

  • claude.ai: paste the code and one existing test for style.
  • Claude Code: the best fit, because Claude can read the module, find existing tests, and run the suite. Save it as a skill at .claude/skills/write-tests/SKILL.md and pass the file path with $ARGUMENTS; see the skills guide. For a hard gate, a Stop hook can run your test command before Claude finishes; see the hooks guide.
  • API: send the code first and the instructions after it.

Variables

VariableWhat to put there
{{CODE}}Function, class or module under test
{{FRAMEWORK}}“TypeScript + Vitest”, “Python + pytest”, “Go testing”
{{EXAMPLE_TEST_OR_NONE}}One existing test file, or “none”
{{TEST_COMMAND}}npm test, pytest -q, go test ./...

Example output (illustrative)

## Test plan
| Case | Input | Expected | Type |
| empty cart | [] | total 0 | boundary |
| 100% discount | price 50, discount 1.0 | total 0, no negative | boundary |
| negative quantity | qty -1 | throws ValidationError | invalid input |

## Suspected bugs
- A discount above 1.0 produces a negative total; no validation exists.

Tips and variations

  • Characterization tests: before a refactor, ask Claude to test current behavior exactly, bugs included, and label them. Then use the refactor plan prompt.
  • Property-based tests: ask for properties (for example “sorting is idempotent”) using your language’s property-testing library.

Frequently asked questions

Why does the prompt plan test cases before writing code?

A visible case list lets you spot missing scenarios in seconds. It also stops the model from writing many near-duplicate tests for the happy path while skipping the edge cases.

What if a generated test fails?

Treat it as information. Either the test is wrong or it found a bug. The prompt asks Claude to flag suspected bugs rather than change the test to match the current behavior.

Should Claude run the tests?

Yes, whenever it can. In Claude Code, give it the test command and ask it to run the suite and show the output. A test suite that was never executed is not verified.

Get the weekly Claude digest

New skills, MCP servers, prompts and Claude updates — one short email a week. No spam, unsubscribe anytime.

The newsletter is launching soon. Follow the RSS feed in the meantime.