P

GitHub Copilot Prompt to Generate Unit Tests

GitHub Copilot has become an indispensable ally for developers looking to speed up writing unit tests. Thanks to its direct integration into the code editor, it analyzes the context of your source files to suggest relevant tests covering edge cases. However, the quality of generated tests heavily depends on the precision of the prompt used. A vague prompt will produce superficial tests, while a structured prompt will guide Copilot toward exhaustive coverage including normal cases, expected errors, and boundary behaviors. In this guide, we offer an optimized prompt to generate robust unit tests with GitHub Copilot, regardless of your tech stack. You'll discover how to formulate your instructions to obtain tests that respect your project's conventions, use the right test framework, and cover critical scenarios you might forget. Whether you work with JavaScript using Jest, Python with pytest, or Java with JUnit, these prompts adapt to your development environment.

The prompt

GitHub Copilot

Generate comprehensive unit tests for the selected function/class following these guidelines:

  1. Framework: Use [Jest/pytest/JUnit/other] with project conventions
  2. Structure: Organize tests using the Arrange-Act-Assert (AAA) pattern
  3. Coverage: Must include:
    • Happy path with realistic data
    • Edge cases (empty values, null, undefined, zero, empty strings)
    • Error cases (invalid inputs, expected exceptions)
    • Boundary cases (min/max values, empty vs single vs multiple items)
  4. Mocks: Create mocks for external dependencies (API, database, files)
  5. Naming: Name each test according to the pattern "should [EXPECTED_BEHAVIOR] when [CONDITION]"
  6. Assertions: Use precise, specific assertions, not simple toBeTruthy()
  7. Isolation: Each test must be independent with its own setup/teardown if necessary

Generate at least 8 tests covering all these scenarios.

Personalize this prompt with Léa

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

Why it works

This prompt is effective because it provides Copilot with a structured framework that eliminates ambiguity: the AAA pattern, case categories to cover, and naming conventions guide generation toward professional tests. By explicitly specifying edge and error cases, you force Copilot to go beyond the trivial tests it would produce by default. Mentioning the framework and a minimum number of tests anchors the response in a concrete and measurable technical context.

Expected result

Copilot generates an organized and complete unit test suite, with at least 8 tests covering normal cases, edge cases, and error cases. Each test follows the Arrange-Act-Assert pattern with descriptive naming, mocks for external dependencies, and precise assertions that granularly verify expected behavior.

Variants by level

FAQ

How to adapt the prompt to the test framework used in my project?
Replace the framework mention in square brackets with the one you actually use (Jest, Vitest, pytest, JUnit, NUnit, etc.). Copilot will automatically adapt to the conventions and syntax of the specified framework. For best results, ensure your test file is already created with the framework import at the top: Copilot will use this context to stay consistent with your configuration.
Can GitHub Copilot generate tests for code with complex dependencies like a database?
Yes, but you need to guide it explicitly. Mention in your prompt that external dependencies should be mocked and specify the desired mock type (jest.mock, unittest.mock, Mockito). For complex cases like database connections or API calls, add a line like "Create a mock for the repository that returns realistic test data." Copilot will then generate appropriate stubs with consistent fake data.
How can I ensure that Copilot-generated tests cover my code sufficiently?
After generation, run your code coverage tool (Istanbul/nyc for JavaScript, coverage.py for Python, JaCoCo for Java) to identify uncovered branches. You can then run Copilot again with a targeted prompt: "Add tests to cover the missing branches: [paste uncovered lines]." The goal is not to reach 100% coverage at all costs, but to ensure critical paths and error cases are well tested.

Related prompts

How to use this prompt

  1. Copy the prompt with the button above.
  2. Paste it into ChatGPT, Claude or your favorite AI assistant.
  3. Replace the bracketed variables with your details, then refine the result.

About Prompt Guide

Prompt Guide is a free library of 2500+ ready-to-use prompts for ChatGPT, Claude and other AIs, with guides to learn prompting and tools to build and optimize your own prompts.

More prompts to explore

GitHub Copilot Prompt to Optimize a Conversion Rate

GitHub Copilot, the AI-powered coding assistant, is not limited to classic code generation. In the context of conversion rate optimization...

GitHub Copilot Prompt to Optimize a Landing Page

GitHub Copilot, the AI-powered coding assistant from GitHub, is not limited to writing functions or unit tests. Used intelligently, it becomes a formidable ally for optimizing landing pages—those crucial pages that turn visitors into customers.

GitHub Copilot Prompt to Optimize a Workflow

GitHub Copilot has become an essential assistant for developers looking to optimize their development workflows. Whether you are working on

GitHub Copilot Prompt to Refactor Code

Code refactoring is a crucial step in software development that involves restructuring existing code without changing its external behavior.

Midjourney Prompt for Analyzing a Contract

Midjourney, as an AI image generation tool, cannot analyze the textual content of a contract in the legal sense. However, it excels at creating impactful visuals to illustrate complex contractual concepts: negotiation process infographics, visual representations of key clauses, decision flow diagrams, or professional illustrations for legal presentations. These visuals transform dry documents into engaging communication materials, making them easier to understand for non-legal audiences. Whether you are a lawyer preparing a client presentation, a legal officer training your teams, or an entrepreneur looking to simplify your terms and conditions, Midjourney allows you to create professional-quality visual representations. The following prompts are designed to generate clear, structured illustrations that are appropriate for the legal and contractual context, respecting the visual codes of the sector: sobriety, clarity, and professionalism. Discover how to turn contract analysis into a visual experience with optimized prompts for Midjourney.

Midjourney Prompt for Creating E2E Tests

Midjourney, renowned for its power in AI image generation, can play an unexpected but strategic role in the creation of E2E tests.

Go further

Get new prompts every week

Join our newsletter.