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 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.

Make it specific to you

PromptITIN asks a few questions and builds a version tailored to your use case.

✦ Enhance with AI

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 PromptITIN 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 195 templates