How Do You Get ChatGPT to Write Tests With Its Code?
Ask for the tests in the same request as the code, and name the edge cases yourself. Then run them. In Chat, you run them on your machine. In Codex, ask it to run them and report the commands and results.
Write [function or feature] in [language and version], using [test framework]. Required behavior: - [behavior 1] - [behavior 2] First, list the edge cases you plan to test, including [empty input / boundary values / invalid values / anything else you know matters]. Wait for my OK. Then write the tests before the implementation. Each test should fail for one clear reason if the behavior is wrong. Don't mock [the part that must be real]. In your final message, list the commands to run. If you ran them, give the exit code and the output. If you didn't, say "not run". Never describe a test as passing if it wasn't run.
- 1List the behaviors
- 2Write the tests
- 3See them fail
- 4Code until they pass
- LimitsDefault, minimum and maximum
- Odd valuesZero, negative, fractional
- Bad inputNon-numeric values
- Result edgesEmpty results, exactly full pages
Try it now
- 1Paste the prompt into New chat with one real function you need, and approve or edit its list of edge cases.
- 2Run the tests on your machine before adding the implementation. In Codex, ask it to run them and show the output.
- 3Break the code on purpose, by changing one condition, and run them again. It worked if a test fails and its name tells you what broke.
Use it when
For anything with logic or edge cases: date handling, pagination, money, parsing, permissions. It's also worth it when you plan to edit the code later, since the tests will show if a future change breaks it. Free and Go include Codex in the desktop app, subject to rollout, while the CLI and IDE extension start at Plus.
Skip it when
A throwaway script you'll run once and check by eye doesn't need a test suite. Skip it for pure layout changes too, where a screenshot tells you more. A test that only confirms a CSS class exists adds upkeep and proves nothing.
Why it works
Code that reads well can still be wrong, and tests turn that doubt into something you can run. The difference between the two surfaces matters here. A ChatGPT chat doesn't have your repository. OpenAI's Projects page says a ChatGPT project has no direct access to a folder on your computer, so code written there hasn't run against your project. Codex can read your repo and run commands, within its sandbox.
OpenAI's Prompting guide gives a short Codex example: "Write a unit test for this function. Follow conventions used in other tests." Its use case on turning an issue into a tested pull request goes further. It asks for tests that separate required behavior from old behavior, and it tells the agent to separate existing failures from new ones and never describe an unrun check as passing. Its sample proves this with a run that's expected to fail on the unfixed code.
Example
You need a parse_price(text) function in Python 3.12 with pytest. You list "$1,299.50", "1299", an empty string and "free" as cases, and say that invalid input must raise ValueError and never return 0. That last rule matters most. It's the kind of decision ChatGPT would otherwise make for you, and a test written after the code tends to lock in whatever it chose.
Going further
OpenAI's tested pull request use case offers a checklist worth borrowing: default, minimum and maximum limits; zero, negative, fractional and non-numeric values; empty results and exactly full final pages. It also records each requirement with its command, exit code and status, where blocked or unrun checks don't count as passing. Ask for that table in your prompt.
Then run /review, which OpenAI says reports prioritized findings on your changes without touching your working tree. The review_model setting in config.toml lets a different model do the review from the one that wrote the code.
Common mistakes
- Accepting "all tests pass" from a chat that had no way to run them. Ask whether they ran, and look at the output.
- Letting the code and tests share one misunderstanding. If you don't name the cases yourself, the tests can only confirm what the code already does.
- Letting Codex edit a failing test to get it passing. Add "Don't change existing tests without asking" to your prompt.
- Ignoring your own conventions, so generated tests use a different style and framework. Point to an existing test file.
Sources
- ChatGPT Learn: Prompting
- OpenAI use case: Turn an issue into a tested pull request
- ChatGPT Learn: Projects and chats
- ChatGPT Learn: Code review
- ChatGPT Learn: Pricing
Checked 6 November 2026. Products change often, so confirm details with the vendor.