Write Comprehensive Unit Tests
Generate an exhaustive unit test suite covering nominal cases, edge cases, and errors with appropriate mocks.
Paste in your AI
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:
- Happy paths: normal usage scenarios where everything works as expected.
- Edge cases: null values, empty strings, empty arrays, negative numbers, maximum values.
- Error cases: expected exceptions, network errors, timeouts, malformed data.
- Mocks and stubs: mock external dependencies (databases, APIs, third-party services) with realistic implementations.
- 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
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 OptimizerComments
Be the first to comment on this prompt.
Glossary Terms
📬 Get new prompts every week
Join our newsletter and never miss a prompt.
Go further
Improve this prompt with the Optimizer
Analyze the prompt, strengthen the context and get a more precise version.
Prompt Engineering
Cross-model principles for writing more reliable prompts.
AI prompts for web developers
Code review and debugging, keeping API keys and secrets out of the prompt.
Security, GDPR & Business Plans
ChatGPT Business, Claude Team, Gemini Workspace and Mistral Vibe compared from vendor sources: training, EU hosting and a GDPR checklist.
Similar Prompts
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.
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.
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.
Generate Storybook Stories for UI Components
Generate Storybook stories for a UI component, with visual variants, interactive controls, interaction tests, and MDX documentation.