P
💻DeveloppementIntermediateChatGPT

ChatGPT Prompt to Generate Unit Tests

Writing unit tests is a crucial step in software development, but it is often seen as time-consuming and repetitive. ChatGPT radically transforms this practice by allowing you to generate complete test suites in seconds. Whether you work in JavaScript, Python, Java, or any other language, AI can analyze your source code, identify edge cases, and produce tests covering both nominal scenarios and error cases. The challenge is not just to produce test code, but to ensure relevant coverage that actually detects regressions. A well-structured prompt enables ChatGPT to understand the business context of your function, the dependencies to mock, and the assertions to verify. Developers who master this approach reduce their test writing time by 60-80%, while improving the quality of their coverage. In this guide, you will find optimized prompts to generate professional unit tests, tailored to your level of expertise and tech stack.

Paste in your AI

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

You are a senior QA engineer specialized in unit tests. Generate a complete unit test suite for the following code:

[PASTE YOUR CODE HERE]

Technical context:

  • Language: [LANGUAGE]
  • Test framework: [Jest/Pytest/JUnit/other]
  • Mock library: [if applicable]

Requirements:

  1. Cover all execution paths (happy path, edge cases, errors)
  2. Use the AAA pattern (Arrange, Act, Assert) for each test
  3. Name each test with the convention: should_[expected_result]when[condition]
  4. Mock external dependencies (API, database, file system)
  5. Include tests for null, undefined, empty strings, and incorrect types
  6. Add an explanatory comment above each describe/test block
  7. Aim for 100% branch coverage

Output format: ready-to-run code with necessary imports.

Personalize this prompt with Léa

Answer 3 questions and Léa tailors the prompt to your situation.

Why this prompt works

This prompt leverages the QA expert role to activate ChatGPT's specialized knowledge of testing best practices. The numbered requirements structure forces exhaustive and methodical coverage, while the naming convention and AAA pattern ensure readable and maintainable tests. The explicit technical context eliminates any ambiguity about the framework to use.

Use Cases

Generate Unit Tests

Variants

Expected Output

ChatGPT generates a complete and executable unit test suite, organized into logical describe blocks with tests covering normal operation, edge cases, and error scenarios. Each test follows the AAA pattern with descriptive names, external dependencies are properly mocked, and the code includes all necessary imports for immediate execution.

Frequently Asked Questions

Are tests generated by ChatGPT reliable and production-ready?

Generated tests provide an excellent starting point, but they always require human review. ChatGPT can produce incorrect assertions if it misinterprets business logic, or miss cases specific to your domain. Best practice is to generate the tests, run them immediately to verify they pass, then adjust assertions and add business cases the AI didn't anticipate. On average, 70 to 85% of the generated code is directly usable.

How can I get tests for code with many external dependencies?

Explicitly specify in your prompt the dependencies to mock and the mocking library used (Mockito, unittest.mock, jest.mock, etc.). If possible, provide the interfaces or signatures of the dependencies so ChatGPT generates realistic mocks. For complex cases like databases or REST APIs, ask ChatGPT to create reusable fixtures and test data factories rather than hard-coded values in each test.

What's the best way to improve code coverage with ChatGPT?

Start by generating a coverage report with your usual tool (Istanbul, coverage.py, JaCoCo), then paste the uncovered lines into ChatGPT, specifically requesting tests for those missing branches. You can also ask ChatGPT to analyze your code and list all possible execution paths before generating tests, which ensures a systematic approach rather than testing only the obvious cases.

Learn more

Check the full skill on Prompt Guide to master this technique from A to Z.

View on Prompt Guide

📬 Get new prompts every week

Join our newsletter and never miss a prompt.

Similar Prompts

💻DeveloppementIntermediateAll AIs

Generate Mocks and Fixtures for Your Automated Tests

A prompt to automatically generate realistic mocks, stubs and data fixtures adapted to your test framework and use cases.

091
💻DeveloppementIntermediateAll AIs

Automatically Generate Unit Tests with AI

Automatically generate an exhaustive unit test suite covering nominal cases, edge cases, and error cases for any source code.

0223
💻DeveloppementIntermediateGemini

Create a Python Automation Script

Create a professional Python automation script with CLI configuration, structured logging, error handling, and tests.

24239
💻DeveloppementAdvancedAll AIs

Analyze and Optimize Algorithmic Complexity

Analyze the Big O complexity of your algorithms and optimize them with appropriate data structures and more efficient algorithms.

40233