testing
Generate a thorough unit test suite for existing code
Generates a unit test suite that actually covers edge cases and failure paths for existing code, instead of only testing the happy path.
Who should use this
Is this prompt for you?
- Developers adding tests to existing, currently-untested code
- Teams that want consistent test coverage before a refactor
- Anyone who has received an AI-generated test suite that only checked the obvious case
The prompt
Copy this prompt
Prompt
Write a thorough unit test suite for the following code.
Code to test:
[paste the function(s)/module/class]
Testing framework: [Jest / Vitest / pytest / etc.]
Mocking approach: [describe how external dependencies should be mocked, e.g. "mock the database client", "mock fetch calls"]
Before writing tests, list out loud what you're going to test: the happy path, then every edge case and failure mode you can identify from the code (invalid input, empty input, boundary values, null/undefined, concurrent calls, network/dependency failures, permission/auth failures if relevant).
Then write the tests:
1. Group them logically with describe/context blocks matching the behavior being tested, not the internal implementation
2. One assertion focus per test — avoid tests that check five unrelated things at once
3. Use descriptive test names that state the expected behavior ("returns an error when the email is invalid", not "test 3")
4. Mock external dependencies (network, database, filesystem, time/Date) so tests are deterministic and fast
5. Include at least one test that would fail if a specific known bug pattern were reintroduced (e.g. an off-by-one, a race condition, a missing null check) — comment why that test exists
6. Avoid testing implementation details (private internals, exact call counts to internals) that would break on a harmless refactor
After writing the tests, tell me:
- What coverage gaps remain that you couldn't test from the code alone (e.g. things that need a real integration environment)
- Whether anything in the source code looks untestable as currently structured, and whyWorks well with Claude Code, Cursor and OpenAI Codex.
How to use it
Getting the best result
- 1Paste the actual code being tested, including its type signatures and any dependencies it calls
- 2Specify your real testing framework and mocking library so the output runs without changes
- 3Read the "what I'm going to test" list before the tests are generated and add any case specific to your domain
- 4Run the generated suite immediately — a test suite that doesn't pass on the actual code as-is needs to be treated as broken, not the code
Expected result
What you should get back
- An explicit list of happy-path, edge-case, and failure-mode scenarios identified before any test code is written
- A well-organized test suite with descriptive names and mocked external dependencies
- At least one regression test tied to a specific bug pattern, with a comment explaining why it exists
- An honest list of coverage gaps that need integration-level testing instead of unit tests
Tips
Get more out of this prompt
- Ask for the scenario list before the code — it's much faster to add a missing case at that stage than after 40 lines of test code exist
- Explicitly ask it to avoid testing private implementation details so tests don't become brittle during refactors
- If the code touches time, randomness, or the network, confirm those are actually mocked, not silently making real calls in tests
Common mistakes
What to watch out for
- Accepting a test suite that only covers the happy path because no edge cases were requested explicitly
- Letting tests assert on internal implementation details, which then break on any harmless refactor
- Not running the generated tests before trusting them — a test that was never executed provides zero actual verification
Related prompts
Keep going
react
Refactor a messy React component into clean, reusable pieces
Guides an AI to safely refactor a large, tangled React component into smaller composable pieces while preserving exact behavior.
Copy promptdebuggingDebug a React state bug with a systematic root-cause prompt
Forces a systematic root-cause investigation of a React state or rendering bug instead of a surface-level patch that might mask the real issue.
Copy promptpythonWrite a robust Python automation script with error handling
Produces a Python automation script that handles real-world edge cases — bad input, network failures, partial completion — instead of a fragile happy-path-only script.
Copy promptReady to put this prompt to work?
Pair it with an AI coding tool and a real project, then ship something this week.