Prompt Library coding intermediate

ChatGPT Prompts for Unit Tests: Complete Test Coverage Fast

ChatGPT prompts for writing unit tests. Complete test suites for any function or class, edge case coverage, and mock setup for JavaScript, Python, and more.

Tested on: GPT-4oClaude 4Gemini 2.5

The Prompt

Act as a test-driven development expert who maintains 95%+ coverage on production codebases.
Write comprehensive unit tests for:
Code to test: [PASTE FUNCTION/CLASS/MODULE]
Language and test framework: {e.g. TypeScript + Jest, Python + pytest, Go + testing package}
Coverage requirements: {happy path only / edge cases / full coverage}
Mocking needs: {what dependencies need to be mocked — APIs, databases, file system}
Existing test style (if any): [PASTE EXAMPLE TEST — or "none"]

Test output:
1. Test file (complete, runnable — includes all imports, describe blocks, and beforeEach/afterEach if needed)
2. Test cases coverage map:
   | Test name | What it tests | Coverage category |
   (categories: happy path / edge case / error case / boundary / integration point)
3. Missing coverage callout (what this test suite still doesn't cover and why it matters)
4. Mock setup explanation (for complex mocks — explain why each dependency is mocked this way)

Test case requirements:
- At least 1 test for: valid inputs, invalid inputs, boundary values, error states
- Each test: one assertion concept per test (Arrange-Act-Assert pattern)
- Test names must read as documentation: "should return null when input is empty array"
- No "magic numbers" — use named constants for test values

Constraints:
- All tests must be runnable as-is — no placeholder comments
- Mocks must be typed correctly for the language
- Test names must describe behavior, not implementation ("should calculate discount correctly" not "test discountCalculator")
- Assert on outcomes, not implementation details

Variables to fill in

  • {code to test} The function, class, or module — paste the full code
  • {test framework} Language and test framework (Jest, pytest, etc.)
  • {mocking needs} What external dependencies need to be mocked
  • {existing test style} Paste an example test to match style, or 'none'

How to use this prompt

  1. Paste the function you want to test and run — the output is copy-paste ready
  2. Use the coverage map to verify all critical paths are tested before PR
  3. Start with happy path + error cases first, then add edge cases
  4. Use the missing coverage section as your test backlog for future sprints
Test suite running in terminal with green passing indicators
Photo by Chris Ried on Unsplash

Tests are documentation that never lies

Unlike comments (which go stale) and README files (which get ignored), unit tests describe exactly how code behaves — because they run against the actual code. A test named ‘should return 0 when cart is empty’ tells you more about the function’s expected behavior than any documentation.

One assertion per test is a discipline, not a limitation

Tests that assert multiple things are hard to diagnose when they fail — you can’t immediately tell which assertion failed and why. One assertion per test means that a failing test is a complete, specific diagnostic: exactly one thing went wrong, and you know immediately what it is.

Boundary values are where bugs hide

The happy path test (‘valid input → expected output’) rarely finds bugs. The bugs are at the edges: empty arrays, zero values, null inputs, inputs at exactly the maximum allowed size. The prompt’s requirement for at least one boundary value test per function is where it catches the bugs that make it to production.