Coding & Automation prompt

Best AI Prompt for Writing Unit Tests That Catch Real Bugs

CursorClaude CodeChatGPTGitHub Copilot
The short answer

Generates tests that probe edge cases and failure paths instead of re-asserting the happy path you already know works. Copy the prompt below, replace the 5 [BRACKETED] variables with your own, and paste it into Cursor, Claude Code, ChatGPT or GitHub Copilot.

The prompt
You are a test engineer who writes small, fast, readable tests that catch real regressions.

CONTEXT
- Language and test framework: [LANGUAGE_AND_TEST_FRAMEWORK]
- Code under test:
[CODE_UNDER_TEST]
- What this code is supposed to do (business rules, not just the implementation): [INTENDED_BEHAVIOR]
- Dependencies that should be mocked or faked (database, network, clock, filesystem): [DEPENDENCIES_TO_MOCK]
- Existing test style to match (paste an example test if you have one): [EXISTING_TEST_EXAMPLE]

TASK
1. Before writing code, list the behaviors worth testing, grouped as: happy path, boundary values, invalid input, error handling, and state or concurrency issues (if relevant).
2. Flag any behavior where the code appears to contradict the intended behavior. These are probable bugs; write a test that would fail and mark it clearly.
3. Write the tests.

RULES
- One behavior per test. Test names describe the behavior in plain language (for example "returns empty list when user has no orders").
- Use Arrange / Act / Assert structure. No logic (loops, conditionals) inside tests unless using the framework's parameterized test feature.
- Test public behavior, not private implementation details.
- Mock only the listed dependencies. Do not mock the code under test.
- Make tests deterministic: no real time, randomness or network.
- Do not invent functions or modules that are not in the code provided. If you need a helper, define it in the test file.

OUTPUT FORMAT
1. Test plan (bulleted list grouped by category).
2. Suspected bugs (if any) with the failing test that demonstrates each.
3. The complete test file, ready to run.
4. One line on what is still untested and why.

What to fill in

Everything in [GOLD CAPS] is yours to replace. Leave one blank and the machine will guess — and the machine guesses like a meatbag on a Monday.

  • [LANGUAGE_AND_TEST_FRAMEWORK]
  • [CODE_UNDER_TEST]
  • [INTENDED_BEHAVIOR]
  • [DEPENDENCIES_TO_MOCK]
  • [EXISTING_TEST_EXAMPLE]

How to use it

  1. Describe the intended behavior in business terms. If you only paste the code, it will faithfully test that your bugs are still there.
  2. Read the test plan before the code. If a category you care about is missing, say so then, before it writes forty tests you will throw away.
  3. Run the tests immediately. A test that has never failed is a decoration; break the code on purpose once to make sure they bite.

Example

LANGUAGE_AND_TEST_FRAMEWORK: JavaScript, Vitest
INTENDED_BEHAVIOR: applyDiscount(cart, code) applies at most one code, never takes the total below zero, rejects expired codes
DEPENDENCIES_TO_MOCK: Date (for expiry)

Works with

Tested for Cursor, Claude Code, ChatGPT, GitHub Copilot. Any modern chat model that follows long instructions will cope; the weaker ones will need you to restate the output format if they drift.

Weapons from the Arsenal that pair well with this prompt:

  • Cursor — AI-first code editor (a VS Code fork) with agents, codebase-aware chat and fast autocomplete.
  • Claude Code — Anthropic's agentic coding tool that reads your codebase, edits files and runs commands from terminal, IDE, desktop or web.
  • GitHub Copilot — GitHub's AI pair programmer: completions, chat and coding agents across editors and GitHub itself.