Any AI ModelIntermediate

AI Prompt to Generate Unit Tests for a Function

Generate a solid set of unit tests covering normal cases, edge cases, and failure cases.

The Prompt

Write unit tests for the following function using [TESTING FRAMEWORK, e.g. Jest, pytest]. Cover: 1) the normal/expected use case, 2) at least 2 edge cases (empty input, boundary values, unusual but valid input), 3) at least 1 failure case (invalid input, error handling). For each test, add a one-line comment explaining what it verifies. Function: [PASTE FUNCTION AND ITS SIGNATURE/TYPES]

Example

Example Input

A TypeScript function calculateDiscount(price: number, percent: number): number using Jest.

Example Output

A test suite with a normal case (20% off $100 = $80), edge cases (0% discount, 100% discount, price of 0), and a failure case testing that a negative percent throws or returns a defined error, each with an explanatory comment.

Tips for Better Results

  • Include the function's type signatures if using TypeScript — this helps the AI generate tests that actually match your expected input/output types.
  • Explicitly ask for edge cases — without prompting, AI models often default to only the happy path.
  • Run the generated tests immediately; treat any that fail unexpectedly as a signal to review the function's actual behavior, not just the test.

FAQ

Do AI-generated tests replace manual QA?

No — they're a strong starting point for coverage, especially for edge cases developers often skip, but manual and integration testing are still important for catching issues unit tests can't reach.

Which testing frameworks does this work with?

This prompt works with any framework — Jest, Vitest, pytest, JUnit, RSpec, and others — just specify the framework and the AI will match its syntax and conventions.

Related Prompts

Any AI Model
AI Prompt to Debug an Error Message Fast
Any AI Model
AI Prompt to Refactor Messy Code Without Breaking It
Claude
Claude Prompt for Thorough Code Review