ChatGPT Testing Prompt Template
You get runnable tests that cover the normal case, the edges and the failure paths, written in your framework and naming style. This version is set up for ChatGPT: upload or paste the material the prompt works on below the instructions. Below you'll find the prompt, how to run it in ChatGPT, a worked example and answers to common questions.
Write tests for the code below. ## Code under test Language and test framework: [e.g. TypeScript with Vitest] [paste the function, class or module] ## What it should do [describe the intended behavior, including rules that are not obvious from the code] ## Coverage I want - The main happy path - Edge cases: [e.g. empty input, very large values, unicode, null] - Error cases: [what should throw or return an error] ## Instructions - Use only the test framework named above, no extra libraries. - One behavior per test, with a test name that says what is expected. - Use realistic input values, not "foo" and "bar". - If the code has a bug that a test would expose, write the test anyway and tell me it will fail. ## Output format 1. The full test file, ready to run 2. A short list of anything you chose not to test and why Context - Reference material: [paste it below, or attach the file]
How to use this prompt
- 1
Open ChatGPT
Start a new chat in ChatGPT and paste the prompt.
- 2
Give it context
Upload or paste the material the prompt works on below the instructions.
- 3
Review and iterate
Reply in the same chat to refine, and quote the part you want changed.
- 4
Save what you repeat
Preferences you want in every chat (tone, audience, format) belong in ChatGPT's custom instructions. For a prompt you reuse with fixed files, a custom GPT or project works too.
Working in ChatGPT
- •Custom instructions apply to every chat, so keep task-specific rules in the prompt, not there.
- •If the reply drifts from the format, paste the format line again and ask it to redo only that part.
- •Start a new chat for unrelated work so earlier messages don't color the answer.
Worked example
What you give it
Language and framework: JavaScript with Vitest
export function slugify(text) {
return text.toLowerCase().trim().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
}
What it should do: turn a title into a URL slug with single hyphens and no leading or trailing hyphens.
Edge cases: punctuation, extra spaces, symbols only.What a good answer looks like (shortened)
import { describe, it, expect } from "vitest";
import { slugify } from "./slugify";
describe("slugify", () => {
it("lowercases and joins words with hyphens", () => {
expect(slugify("Hello World")).toBe("hello-world");
});
it("collapses punctuation runs into one hyphen", () => {
expect(slugify("Rock & Roll!!")).toBe("rock-roll");
});
it("strips leading and trailing hyphens", () => {
expect(slugify(" --Draft-- ")).toBe("draft");
});
it("returns an empty string for symbols only", () => {
expect(slugify("!!!")).toBe("");
});
});
Not tested: accented letters are dropped, so "Café" becomes "caf". Confirm whether that is acceptable before I add a test.Tips for better results
- •The "What it should do" section matters more than the code. Rules like "discounts never stack" are invisible in the source and are exactly what tests should check.
- •List your edge cases explicitly under Coverage I want. The AI tends to stop at the happy path unless you name empty, null and boundary values.
- •Custom instructions apply to every chat, so keep task-specific rules in the prompt, not there.
- •Avoid this: Asking for tests without describing the intended behavior, so the AI writes tests that just lock in whatever the code does today, bugs included.
Get the free prompt pack
Our best coding prompts in one Markdown file, delivered by email. The templates here stay free either way — the pack just saves you the copying.
Variations
Integration tests
Replace Coverage I want with: Test [module] together with a real [database or HTTP layer], and include setup and teardown.
Table-driven
Add this line to Instructions: Use a parameterized test (e.g. it.each or pytest.mark.parametrize) for cases that share the same shape.
Existing test file
Add this section: ## Current tests [paste them] and only add tests for behavior these do not already cover.
Save it as a Retext snippet
Retext is a free text expander for Mac. Save this prompt once, then type ;writetests in any app (ChatGPT, Claude, your editor, a browser tab) and the full prompt is pasted in.
- Click Add to Retext. The prompt installs as a snippet with the trigger ;writetests.
- Change the trigger in Retext if you prefer a different one.
- Type the trigger wherever you want the prompt, then fill in the brackets.
Frequently asked questions
Can AI-written tests be trusted?
Treat them like tests from a new teammate. Run them, read the assertions, and check that each one would fail if the behavior broke. A test that can never fail is worse than no test.
Should this go in custom instructions?
No. Custom instructions apply to every chat. Keep this prompt for the task and paste it (or expand it with a Retext snippet) when you need it.
How many tests should I ask for?
Ask for coverage of behaviors, not a number. One test per rule and per important edge case is usually enough, and it keeps the file readable.
How do I reuse this prompt without copying it every time?
Click Add to Retext to save it as a snippet. Then type ;writetests in any Mac app, including your AI tool, and the full prompt is pasted in.
Related Templates
Get the free prompt pack
Our best coding prompts in one Markdown file, delivered by email. The templates here stay free either way — the pack just saves you the copying.