Back to Prompt Library
Coding
TestingTDDQuality AssuranceCoding

Unit Test Generator

Generates a thorough, well-organized test suite covering happy paths, edge cases, and errors.

Best For

  • Developers adding tests to legacy code who need broad coverage fast without manually enumerating every edge case.
  • Teams enforcing a coverage threshold in CI who need a starting test suite before a release deadline.
  • Solo developers practicing test-driven habits who want a checklist of edge cases they might not think of alone.

Prompt Template

Write a comprehensive unit test suite for the following [LANGUAGE] function/class using [TEST FRAMEWORK, Jest/Pytest/JUnit/etc.]. Code: [PASTE CODE HERE] Include tests for: (1) the happy path (expected inputs producing expected outputs), (2) edge cases (empty strings, null/undefined, zero, negative numbers, maximum values), (3) error cases (invalid inputs that should throw or return errors), (4) any async behavior if applicable. Use descriptive test names in the format 'should [expected behavior] when [condition]'. Add a brief comment explaining what each test group is validating. Aim for at least 90% branch coverage.

Pro Tip

Paste the function's actual signature and any custom types it depends on, because generic placeholder types lead to tests that compile against the wrong shape of data.

Example Output

A sample of what this prompt produces once you fill in the placeholders.

For a calculateDiscount(price, code) function tested with Jest, the output includes a describe block with tests like 'should apply 10% discount when code is valid', 'should return original price when code is null', 'should throw when price is negative', and an async test for a code-lookup call, each with a short comment on what it validates.

Recommended Tools

How to use this prompt

  1. Copy the prompt template using the button above.
  2. Paste it into your preferred AI assistant (ChatGPT, Claude, Gemini, etc.).
  3. Replace all bracketed placeholders like [TOPIC] with your specific details.
  4. Send the prompt and refine the output as needed.