Skip to main content

ChatGPT/Copilot Prompt to Generate Comprehensive Unit Tests

Use GitHub Copilot to generate thorough unit tests: edge cases, mocking strategies, and assertions tailored to your test framework.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt
Generate thorough unit tests for my TypeScript code using Vitest. The code under test is a payment processing service that handles creating charges, processing refunds, handling webhooks. 1) Use Copilot /tests on the selected code, then enhance the generated tests with these additions. 2) Add edge case tests: expired card, insufficient funds, duplicate charge, network timeout. 3) Create mock implementations for Stripe SDK, database repository, email notification service that simulate both success and failure scenarios. 4) Add parameterized or data-driven tests for different currencies, various discount types, multiple tax jurisdictions to avoid repetitive test functions. 5) Test async behavior with proper await patterns and timeout handling. 6) Include negative tests that verify correct error types and messages are thrown. 7) Add integration-style tests that test the database layer using an in-memory test database without mocking it. 8) Target 90% code coverage — identify which branches are missed by the initial test generation.

What this prompt does

This prompt turns Copilot into a unit-test generator that goes well past a naive /tests pass. It is keyed on [language] and [test_framework] so the output uses your real tooling, and it describes the code under test through [code_description] and [functionality] so the generated tests target actual behavior rather than placeholder logic. The instruction explicitly starts from Copilot's own /tests command and then layers on the additions that command usually omits.

The structure works because it sequences the parts most generated suites skip. It uses [edge_cases] to force conditions Copilot tends to ignore, [dependencies] to drive realistic mocks for both success and failure paths, and [parameterized_scenarios] to collapse repetitive tests into a single data-driven case. It also covers async behavior with proper await and timeout handling, negative tests that assert specific error types and messages, an integration-style test across [integration_boundary] that is deliberately kept real rather than mocked, and a [coverage_goal] used to flag which branches the first generation missed. Mocking both the success and failure paths of each dependency is where most of the real coverage comes from, which is why the prompt insists on it.

When to use it

  • You have untested or thinly tested service-layer code and want a thorough suite fast.
  • Copilot's default /tests output covers the happy path but skips edge cases.
  • You need mocks that simulate failure, not just success, for external dependencies.
  • You have many near-identical test cases that should become data-driven instead.
  • You are testing async code and want correct await and timeout handling.
  • You want to push toward a specific coverage target and see which branches stay uncovered.

Example output

Expect a test file or files in your [test_framework] syntax: a base suite, additional edge-case tests for the conditions you named, mock implementations of each dependency with both success and failure variants, parameterized tests for the repetitive scenarios, async tests with proper awaits, negative tests asserting specific error types and messages, and an integration-style test that exercises the real boundary. It usually closes with notes on which branches the initial generation left uncovered relative to [coverage_goal], so you know exactly where to keep going.

Pro tips

  • Be concrete in [functionality] — "creating charges, processing refunds, handling webhooks" yields sharper tests than a vague "payment stuff."
  • List the failure modes you actually fear in [edge_cases]; that is precisely where generated suites are weakest and where bugs hide.
  • Name every external in [dependencies] so Copilot mocks both the success and failure paths, since the failure path is where most real coverage lives.
  • Pick a [coverage_goal] you can defend; chasing 100% often just tests trivial getters instead of meaningful logic.
  • Keep [integration_boundary] genuinely unmocked, such as an in-memory test database, so the integration test actually proves something.
  • Treat the first generation as a draft and re-prompt for the branches it admits to missing rather than accepting the whole suite wholesale.

Frequently Asked Questions

Does this replace writing tests by hand entirely?
No. It bootstraps a thorough suite and pushes Copilot to cover edge cases, mocks, and async paths, but you should review the generated assertions for correctness. Copilot can produce tests that pass without actually verifying meaningful behavior, so a human pass still matters.
How does it handle mocking external services like a payment SDK?
You list the dependencies in `[dependencies]`, and the prompt asks Copilot to mock each one with both success and failure scenarios. Mocking the failure path is emphasized because that is where most real coverage and the trickiest bugs live.
Can I target a specific coverage percentage?
Yes. Set `[coverage_goal]` and the prompt asks Copilot to identify which branches the initial generation misses relative to it. Remember that high coverage does not guarantee good tests, so use the number as a guide, not proof of quality.
Does it support async and timeout testing?
Yes. One step explicitly asks for async behavior with proper await patterns and timeout handling. This matters for service code like webhook or refund processing, where timing and retries are part of the contract being tested.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in GitHub Copilot Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

AI assistant · trained on my work

👋

Hey there!

Quick Actions

WhatsApp Direct line to me

Chat on WhatsApp

+880 1723 741224 · Replies within the hour on working days

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

mejba.13@gmail.com

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support