Coding
AI Prompt for Generating Unit Tests
Generate thorough unit tests with this AI prompt: happy paths, edge cases and error paths for your function, in your test framework's idioms.
The prompt
Role: You are a testing expert who writes unit tests that actually catch bugs — not tests that just inflate coverage. Context: Language: [LANGUAGE], test framework: [FRAMEWORK]. Function under test: [FUNCTION_CODE]. Its intended behavior: [INTENDED_BEHAVIOR]. Dependencies to mock: [DEPENDENCIES]. Task: 1) Write tests covering: happy path, boundary values, edge cases (empty, null, zero, max), and error paths. 2) Use [FRAMEWORK] idioms (describe/it, arrange-act-assert). 3) Give each test a descriptive name stating what it verifies. 4) Mock [DEPENDENCIES] properly — test the unit, not its collaborators. 5) End with a list of behaviors you chose NOT to test and why. Format: Test file in a code block, then the not-tested list with reasons. Ask the AI to rank the generated tests by bug-finding value and to mark any that merely restate the implementation. Delete the low-value ones — a lean suite that catches bugs beats a large one that only inflates coverage. Constraints: Test behavior, not implementation — tests should survive refactoring. One assertion concept per test. No testing of mocks themselves or trivial getters.
Variables to fill in
Replace each [variable] in the prompt with your own details:
[LANGUAGE] [FRAMEWORK] [FUNCTION_CODE] [INTENDED_BEHAVIOR] [DEPENDENCIES]
Example input
LANGUAGE=TypeScript, FRAMEWORK=Vitest, FUNCTION=calculateDiscount(price, userType) with tiers, DEPENDENCIES=none, STYLE=describe/it blocks
How to use this prompt
Paste the function AND its spec — tests should verify intent, not just current behavior. Name the test framework; syntax differs.
Tips for the best results
- Review every assertion against the SPEC, not the code — generated tests can lock in buggy behavior.
- Ask for the 'not tested' list; it reveals the AI's blind spots for you to cover.
- Run the tests, then deliberately break the function to confirm they fail (mutation check).
Frequently asked questions
How do I know the generated tests are correct?
It generates tests from the code's behavior, but you must verify each assertion matches the intended spec — AI tests can enshrine bugs.
How many tests are enough for one function?
Cover the happy path, boundaries, and error paths — usually 5 to 10 focused tests. The prompt's 'not tested' list tells you what you deliberately skipped.