20 tested prompts across 4 stages. Works with ChatGPT, Claude, and Gemini.

Most people try to use AI for ChatGPT for Unit Tests with a single vague prompt and get generic results. This guide takes a different approach: 4 targeted stages, from Plan your tests through Build a testing habit, each with a prompt that gives the AI exactly the context it needs. Write comprehensive unit tests faster using ChatGPT to improve coverage and catch bugs before they reach production. Works with ChatGPT, Claude, and Gemini.
Stage 1
Start here to map out what needs testing before you write the first test.
Identify what to test
Here is my code: [PASTE CODE]. What should I test? List the key behaviors, edge cases, and failure modes that unit tests should cover. Prioritize by risk.
Choose testing patterns
What testing approach should I use for [DESCRIBE CODE TYPE: PURE FUNCTIONS / REACT COMPONENTS / API HANDLERS / SERVICES]? Recommend the right structure and what to mock.
Plan mocking strategy
This code depends on: [LIST DEPENDENCIES]. What should I mock and what should I leave as real implementations? Suggest a mocking strategy that keeps tests fast without over-mocking.
Set up a test file
Set up a test file for [FILE NAME] using [JEST / VITEST / PYTEST / OTHER]. Create the basic structure with imports, describe blocks, and placeholder tests I should fill in.
Plan coverage goals
For this module: [PASTE CODE], which parts are most critical to test and which are lower priority? Be practical about coverage goals — I don't have unlimited time.
Stage 2
These prompts help you write tests that catch real bugs and stay easy to maintain.
Write tests for a function
Write unit tests for this function using [TESTING FRAMEWORK]: [PASTE FUNCTION]. Cover: the happy path, edge cases (empty input, null, boundary values), and error cases.
Write tests with mocks
Write tests for this code that mock its dependencies: [PASTE CODE]. Mock [LIST DEPENDENCIES] and test that the code correctly calls and handles responses from each dependency.
Write async tests
Write tests for this async function: [PASTE ASYNC CODE]. Cover: successful resolution, error/rejection handling, and any timeout behavior.
Write parameterized tests
Write parameterized tests for this function that cover multiple input/output combinations: [PASTE FUNCTION]. Use test.each or equivalent to test all cases with less code.
Test a React component
Write tests for this React component using [TESTING LIBRARY]: [PASTE COMPONENT]. Test: initial render, user interactions (click, type, submit), state changes, and error states.
Stage 3
Use these prompts to strengthen your tests and remove brittle or redundant coverage.
Review existing tests
Review these tests and tell me what is missing: [PASTE TESTS]. Are they testing the right things? What edge cases are not covered? Would they catch common bugs?
Fix flaky tests
This test is flaky — it sometimes passes and sometimes fails: [PASTE TEST]. What are the most likely causes and how do I fix it?
Reduce test duplication
These tests have repeated setup code: [PASTE TESTS]. Refactor them using beforeEach, shared fixtures, or helper functions to reduce duplication while keeping tests readable.
Improve test naming
Improve the names of these tests to be more descriptive: [PASTE TESTS]. Good test names read like documentation of the expected behavior.
Add test coverage for uncovered code
These lines/branches are not covered by existing tests: [DESCRIBE UNCOVERED CODE]. Write tests that cover them specifically.
Stage 4
Use these prompts to make regular testing a natural part of how you write code.
Write tests first
I want to use TDD for [DESCRIBE FEATURE]. Write the tests first, before any implementation. Use [TESTING FRAMEWORK]. I'll write code to make these pass.
Write regression tests for a bug
I just fixed this bug: [DESCRIBE BUG, PASTE FIXED CODE]. Write regression tests that would have caught this bug before it reached production.
Create test helpers
I have repeated patterns in my tests: [DESCRIBE THE PATTERN]. Create test helper functions or factories that reduce this duplication.
Document my test strategy
Write a brief testing strategy for this codebase: [DESCRIBE PROJECT]. Cover: what to test, how to mock, what to use for integration tests, and coverage goals.
Review PR tests
Review the tests in this PR: [PASTE TESTS AND CHANGES]. Do the tests match the scope of the code changes? What is missing?
Paste your implementation code and ask ChatGPT to write tests for it. It will generate relevant test cases, suggest edge cases you might miss, write the mock setup, and follow your testing framework's patterns. What might take 30 minutes to write manually can be generated in seconds.
ChatGPT can write tests in any framework: Jest, Vitest, Mocha for JavaScript; pytest, unittest for Python; Go's standard testing package; RSpec for Ruby; and many others. Specify your framework in the prompt.
Provide the complete function or module code, specify the testing framework, and describe any important behavior or constraints. Review the output before running — check that mocks match your actual dependencies and test assertions match real expectations.
Test behavior: what the code does, not how it does it internally. Tests that check internal implementation break when you refactor, even if the behavior is unchanged. Test inputs, outputs, and side effects.
Yes. This is one of the highest-value uses. Give ChatGPT your existing untested code and ask it to write characterization tests that document the current behavior. Use these as a safety net before refactoring.
AI Prompts for ChatGPT for Debugging
Diagnose and fix bugs faster by using ChatGPT as a debugging partner across any programming language..
See promptsAI Prompts for ChatGPT for Code Review
Use ChatGPT to catch bugs, improve code quality, and prepare better code before human reviewers see it..
See promptsAI Prompts for Cursor for Unit Tests
Write comprehensive unit tests faster using Cursor to improve code coverage and catch bugs before they reach production..
See prompts