AI Prompts for ChatGPT for Unit Tests

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

AI Prompts for ChatGPT for Unit Tests
Scroll to explore

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

Plan your tests

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.

Plan your tests

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 your tests

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.

Plan your tests

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 your tests

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.

Plan your tests

Stage 2

Write tests

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

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 tests

Write async tests

Write tests for this async function: [PASTE ASYNC CODE]. Cover: successful resolution, error/rejection handling, and any timeout behavior.

Write tests

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.

Write tests

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.

Write tests

Stage 3

Improve test quality

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?

Improve test quality

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?

Improve test quality

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 quality

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.

Improve test quality

Add test coverage for uncovered code

These lines/branches are not covered by existing tests: [DESCRIBE UNCOVERED CODE]. Write tests that cover them specifically.

Improve test quality

Stage 4

Build a testing habit

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.

Build a testing habit

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.

Build a testing habit

Create test helpers

I have repeated patterns in my tests: [DESCRIBE THE PATTERN]. Create test helper functions or factories that reduce this duplication.

Build a testing habit

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.

Build a testing habit

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?

Build a testing habit

Frequently asked questions

How does ChatGPT help write unit tests faster?+

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.

What testing frameworks does ChatGPT support?+

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.

How do I get ChatGPT to write tests that actually work?+

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.

Should I test implementation details or behavior?+

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.

Can ChatGPT write tests for code that has no tests at all?+

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.