Home/Templates/Write Unit Tests
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.

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

🔒

Upgrade to see the full prompt

Get unlimited access to all 215+ templates — starting at $9/month.

✦ Upgrade to Pro

Already have an account? Sign in

How to use this template

1

Copy the template

Click the copy button to grab the full prompt text.

2

Fill in the placeholders

Replace anything in [BRACKETS] with your specific details.

3

Paste into any AI tool

Works with ChatGPT, Claude, Gemini, Cursor, and more.

4

Or enhance with AI

Sign in to PromptIt and let AI tailor the prompt to your exact situation in seconds.

Why this prompt 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.

Tips for best results

  • Paste the function signature AND a description of the intended behaviour separately — the AI will catch discrepancies between them
  • Add 'include tests that document what the function explicitly does NOT do' to RULES — these are the tests that catch scope creep later
  • Run the generated tests immediately and fix any that fail due to import paths or syntax before trusting the logic
  • Ask the AI to generate a failing test first (TDD style) when designing a new function — it forces clearer specification

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 →
← Browse all 215 templates