P
💻DeveloppementIntermediateAll AIs

Write Comprehensive Unit Tests

Generate an exhaustive unit test suite covering nominal cases, edge cases, and errors with appropriate mocks.

Paste in your AI

Adapt this prompt

Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.

You are an expert in Test-Driven Development (TDD) and software quality assurance. I need to write comprehensive unit tests for the following code written in [language] with the [e.g. Jest, Pytest, JUnit] testing framework:

[paste the code to test]

Context:

  • Testing framework: [e.g. Jest with TypeScript, Pytest, JUnit 5]
  • Mocking library: [e.g. jest.mock, unittest.mock, Mockito]
  • Target coverage: [e.g. 90% minimum]
  • Test style: [e.g. BDD with describe/it, or AAA (Arrange-Act-Assert)]

Generate a complete test suite that covers:

  1. Happy paths: normal usage scenarios where everything works as expected.
  2. Edge cases: null values, empty strings, empty arrays, negative numbers, maximum values.
  3. Error cases: expected exceptions, network errors, timeouts, malformed data.
  4. Mocks and stubs: mock external dependencies (databases, APIs, third-party services) with realistic implementations.
  5. Async tests: if the code is async, cover both resolved and rejected promises.

For each test, use descriptive names in the format 'should [expected behavior] when [condition]'. Include a comment explaining why each edge case is important to test.

Personalize this prompt with Léa · AI assistant

Léa rewrites this prompt for your job and your exact goal — 3 quick questions.

Why this prompt works

This prompt is effective because it requests systematic coverage across the five most important test categories. By specifying the framework and mock library upfront, the AI produces directly usable code without requiring adaptation. The imposed naming convention ('should [behavior] when [condition]') ensures self-documenting tests that also serve as living documentation of expected code behavior. This is an essential practice in team projects. Requesting explanatory comments for edge cases transforms the tests into a pedagogical tool, helping less experienced developers understand why certain scenarios need testing, not just how.

Use Cases

Writing tests for existing codeTDD trainingIncreasing code coverage

Expected Output

A complete unit test suite with nominal cases, edge cases, errors, mocks, and descriptive names ready to run.

Improve this prompt

Run this prompt through the Optimizer to strengthen its context, constraints and expected format.

Improve this prompt with the Optimizer

Comments

Be the first to comment on this prompt.

📬 Get new prompts every week

Join our newsletter and never miss a prompt.

Go further

Similar Prompts

💻DeveloppementIntermediateClaude

Task brief for Claude Code: frame a feature end to end

Brief template to delegate a complete feature to Claude Code: goal, starting point, constraints, definition of done, working rules and recap format.

1680
💻DeveloppementIntermediateClaude

Write a project's CLAUDE.md: the brief Claude Code reads at every session

Turns your project description into a dense CLAUDE.md: commands, structure, conventions, pitfalls, off-limits areas and situations where the agent must ask before acting.

639
💻DeveloppementAdvancedAll AIs

Design a Secure Webhook API

Design a webhook API with event subscriptions, signed deliveries, retry limits, and a delivery log your support team can inspect.

411
💻DeveloppementIntermediateAll AIs

Generate Storybook Stories for UI Components

Generate Storybook stories for a UI component, with visual variants, interactive controls, interaction tests, and MDX documentation.

473