WriteMyAIPromptFree, no sign-up

AI prompt for writing unit tests

This is a ready-made AI prompt for writing unit tests, written for Claude and free to copy. Tests generated alongside an implementation assert whatever that implementation does, bugs included. Deriving them from the specification instead is what makes a test capable of failing, which is the only property that matters. The prompt below handles that: it is compiled as a coding task, so it carries the structure and the constraints that kind of work needs.

The prompt

Written for Claude. Compiled as a coding task — writing, reviewing, debugging, or refactoring software.

<role>
You are a senior software engineer who writes production-ready, tested code.
</role>

<context>
Specification: {{specification}}. Function signature: {{signature}}. Test framework: {{framework}}.
</context>

<task>
Write unit tests for this specification.
</task>

<constraints>
- Derive every test from the specification, not from any implementation.
- Cover the normal case, boundary values, empty input, and every stated error condition.
- Test names must state the expected behaviour.
- Where the specification is ambiguous, write the test and flag the ambiguity in a comment.
- Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections.
- Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle.
- If you are unsure whether an API, library version, or language feature exists, say so rather than guessing at a plausible-looking name.
</constraints>

<output_format>
Respond with a single fenced code block containing the complete implementation, then a short note covering anything the reader needs to know before running it.
</output_format>

<approach>
Work through the problem step by step before giving your answer. Show the reasoning that actually drives your conclusion rather than a tidy summary written after the fact.
</approach>

<success_criteria>
Before you finish, check the output against every item below and fix anything that fails.

- Every test would fail against a deliberately broken implementation.
</success_criteria>

<before_you_start>
If anything above is unclear, or you are missing information you need, ask up to three specific clarifying questions before producing any output. Do not invent facts, names, numbers, sources, or quotations to fill a gap — if you do not know something, say so plainly.
</before_you_start>

Now, write unit tests for this specification.

Optional: start the reply with ``` to force the output straight into shape.

Open this in the prompt builder to add your own context and see what would improve it most.

What makes writing unit tests hard to prompt for

Tests generated alongside an implementation assert whatever that implementation does, bugs included. Deriving them from the specification instead is what makes a test capable of failing, which is the only property that matters.

Why this prompt works

Background comes before the instruction
The model reads the situation before it learns what to do with it, which stops the instruction being diluted by everything that follows. The task is then restated as the final line, where models weight it most heavily.
Rules a coding task assumes but nobody writes down
Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections. Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle. These are added automatically because leaving them implicit is the most common reason this kind of output disappoints.
Success criteria the model checks itself against
Stating how the output will be judged gives the model something concrete to verify before it finishes. Prompts without criteria produce work that is plausible but incomplete, because nothing defined when it was done.
Permission to ask instead of guess
This brief leaves room for interpretation, so the prompt tells the model to ask before inventing details. That converts a confidently wrong answer into a question you can actually answer.
A prefill that removes the preamble
Starting the reply with ````` forces the output straight into the required shape and removes the "Sure, here is..." opener entirely.

What to replace

PlaceholderWhat to put there
{{specification}}Your specification.
{{signature}}Your signature.
{{framework}}Your framework.

Check the output before you use it

  • Deliberately break the implementation and confirm tests fail. Tests that always pass are worse than none.
  • Check the boundary cases are actual boundaries, not just small numbers.
  • Read the flagged ambiguities — each one is a decision you need to make.

The same prompt for other models

Identical content, packaged the way each model reads most reliably.

ChatGPT

## Role

You are a senior software engineer who writes production-ready, tested code.

## Context

Specification: {{specification}}. Function signature: {{signature}}. Test framework: {{framework}}.

## Task

Write unit tests for this specification.

## Constraints

- Derive every test from the specification, not from any implementation.
- Cover the normal case, boundary values, empty input, and every stated error condition.
- Test names must state the expected behaviour.
- Where the specification is ambiguous, write the test and flag the ambiguity in a comment.
- Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections.
- Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle.
- If you are unsure whether an API, library version, or language feature exists, say so rather than guessing at a plausible-looking name.

## Output format

Respond with a single fenced code block containing the complete implementation, then a short note covering anything the reader needs to know before running it.

## Approach

Work through the problem step by step before giving your answer. Show the reasoning that actually drives your conclusion rather than a tidy summary written after the fact.

## Success criteria

Before you finish, check the output against every item below and fix anything that fails.

- Every test would fail against a deliberately broken implementation.

## Before you start

If anything above is unclear, or you are missing information you need, ask up to three specific clarifying questions before producing any output. Do not invent facts, names, numbers, sources, or quotations to fill a gap — if you do not know something, say so plainly.

Now, write unit tests for this specification.

Gemini

**Task**

Write unit tests for this specification.

**Role**

You are a senior software engineer who writes production-ready, tested code.

**Context**

Specification: {{specification}}. Function signature: {{signature}}. Test framework: {{framework}}.

**Constraints**

- Derive every test from the specification, not from any implementation.
- Cover the normal case, boundary values, empty input, and every stated error condition.
- Test names must state the expected behaviour.
- Where the specification is ambiguous, write the test and flag the ambiguity in a comment.
- Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections.
- Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle.
- If you are unsure whether an API, library version, or language feature exists, say so rather than guessing at a plausible-looking name.

**Output format**

Respond with a single fenced code block containing the complete implementation, then a short note covering anything the reader needs to know before running it.

**Approach**

Work through the problem step by step before giving your answer. Show the reasoning that actually drives your conclusion rather than a tidy summary written after the fact.

**Success criteria**

Before you finish, check the output against every item below and fix anything that fails.

- Every test would fail against a deliberately broken implementation.

**Before you start**

If anything above is unclear, or you are missing information you need, ask up to three specific clarifying questions before producing any output. Do not invent facts, names, numbers, sources, or quotations to fill a gap — if you do not know something, say so plainly.

Now, write unit tests for this specification.

Common questions

Should AI write tests before or after the code?
Before, from the specification. Tests written after the implementation tend to assert its behaviour rather than the requirement, which means they pass on buggy code and provide false confidence.
How do I know AI-generated tests are any good?
Break the implementation on purpose and see whether they fail. A test suite that stays green against deliberately wrong code is testing nothing, and this takes thirty seconds to check.
Which AI model is best for writing unit tests?
All of them handle this; what changes is the packaging. This page shows the same prompt written for Claude, ChatGPT, Gemini. Claude follows XML-delimited structure most reliably, ChatGPT works best with markdown headings, and Gemini prefers the task stated before the material. The content of the prompt is identical in each.
Can I change this prompt for my own situation?
Yes, and you should. Replace the placeholders with your own details, then open it in the builder to add context specific to you. The builder scores what you supply and tells you exactly which missing piece would improve it most.
Why does this prompt include rules I did not ask for?
Because coding tasks carry requirements that experienced practitioners apply automatically and rarely write down. The compiler adds them so the output does not fail on something obvious. Every added rule is listed on the how it works page.

Browse all ready-made prompts, the full prompt library, or read the guides.