Coding

Write Unit Tests Prompt Template

Generate thorough unit test suites covering happy paths, edge cases, and error scenarios. Supports Jest, Pytest, and other frameworks with at least 10 test scenarios.

Last updated

The prompt

ROLE: You are a test-driven development advocate and QA lead with a philosophy that untested code is unfinished code. CONTEXT: A developer needs comprehensive unit tests for a function or class. The tests must go beyond the obvious happy path — they should document the contract of the function, act as living documentation, and catch regressions. Good tests specify behaviour, not just verify it runs. TASK: Write a comprehensive unit test suite for the function or class provided below. Cover at minimum 10 distinct scenarios. RULES: • Each test must have a descriptive name that reads as a specification: "should return null when input is empty string" not "test1" • Cover: the primary happy path, 3+ edge cases (boundary values, empty inputs, large inputs), and 3+ error/exception scenarios • Group tests logically using describe blocks if the framework supports it • Mock external dependencies (API calls, DB queries, filesystem) — never let unit tests make real network calls • Add a comment above each test explaining what contract it's verifying (one sentence) CONSTRAINTS: Output only runnable test code with minimal prose explanation. Use the specified framework exactly. All test assertions must be specific — avoid assertTrue(result !== null) when assertEqual(result, expectedValue) is possible. EDITABLE VARIABLES: • [FUNCTION_OR_CLASS] — the code to test (paste the implementation) • [FRAMEWORK] — e.g. Jest, Pytest, Go test, RSpec, JUnit • [LANGUAGE] — programming language • [KNOWN_EDGE_CASES] — any specific cases you already know about (optional) OUTPUT FORMAT: ```[language] // Test suite with describe blocks and 10+ tests // Each test prefixed with a one-line contract comment ``` Summary table: | Scenario | Type | Expected Result | |----------|------|----------------| [one row per test] QUALITY BAR: If these tests all pass, a developer should feel genuinely confident shipping the function to production. If they don't, the coverage gaps should be obvious from the summary table.

Replace every [BRACKETED] placeholder with your own details before running it.

How this prompt is structured

Output format

Complete runnable test suite (10+ tests) + summary table of scenarios covered

Why this template works

Requiring test names to read as specifications transforms tests from verification tools into documentation. The contract comment above each test forces intentional design rather than coverage-metric-chasing.

Pro tips

#1

Paste the function signature AND a description of the intended behaviour separately — the AI will catch discrepancies between them

#2

Add 'include tests that document what the function explicitly does NOT do' to RULES — these are the tests that catch scope creep later

#3

Run the generated tests immediately and fix any that fail due to import paths or syntax before trusting the logic

#4

Ask the AI to generate a failing test first (TDD style) when designing a new function — it forces clearer specification

Need this tailored to you?

Prompt𝙸t𝙸n rewrites any prompt with role, context, task, constraints and format — free.

Try it free →

More coding templates

Code Review

Get a comprehensive AI code review covering bugs, performance issues, security vulnerabilities, best practice violations, and refactoring opportunities with specific line references.

View →

Debug an Error

Diagnose any code error with a structured breakdown: root cause analysis, step-by-step fix, and prevention strategies for the future.

View →

Explain Code Simply

Translate complex code into plain English with line-by-line explanations, real-world analogies, and edge-case analysis for any skill level.

View →

Refactor Code

Improve code readability and maintainability by applying DRY, single responsibility, and clean naming principles — with a clear explanation of every change.

View →

Related reading

Write Unit TestsGenerate Test CasesCoding PromptsHow to Write Better AI Prompts (Step-by-Step)Claude vs ChatGPT: Key DifferencesMeta-Prompt
Browse all prompt templates