Home › Coding › Write unit tests: how to do it with AI
Write unit tests: how to do it with AI
From official vendor pages; selected tasks also include dated logged-out free-plan trials. Report an error.
Intermediate Risk: Low 💻 Coding
An assistant can write unit tests for code you already have, and an agent can run them and fix the obvious failures. Good tests come from describing the behaviour you expect, not from asking it to make the current code pass.
Who it’s for: Developers adding tests to legacy code, students learning testing, and teams raising coverage before a refactor.
Why low risk: Tests can simply restate what the code does (including its bugs), or mock so much that they test nothing.
ChatGPT has the highest score on our published rubric (94/100) of the 4 tools listed for this task. Codex includes PR review; Free has limited Codex access.
Step by step
- Pick one function or module and write down what it should do, including edge cases.
- Give the assistant the code, your test framework (pytest, Jest, JUnit…) and existing test style.
- Ask for tests that cover normal cases, edge cases and errors, each with a one-line reason.
- Run them. For each failure, decide whether the test or the code is wrong before letting it change either.
- Remove tests that only check mocks or duplicate others.
- Commit tests separately from code fixes so reviews stay clear.
AI prompts to write unit tests
Replace the [brackets] with your details. See all prompt examples
Write [framework] unit tests for this function. Expected behaviour: [rules]. Cover normal input, edge cases ([list]) and errors. One-line comment per test explaining what it protects. [paste code]
Here are the function and its current tests. List behaviours and edge cases the tests don't cover, ranked by risk. Don't write code yet.
Review these tests: which ones would still pass if the function were broken? Which mock too much? Suggest fixes.
Illustrative example
You paste
Function: parse_price("$1,299.00") returns 1299.0.A good answer looks like
Tests suggested: plain number "12" → 12.0; thousands separator; no dollar sign; empty string raises ValueError; negative "-5" (ask: allowed?); European "1.299,00" (ask: supported?). The two questions go back to you instead of guessing.
Illustrative example written for this guide, not output captured from a specific tool.
Which tool to pick, and why
- Claude (incl. Claude Code): Claude Code includes code review and can read the whole repository; the Free plan handles pasted snippets. Visit official website ↗
- Cursor: Its agent can run your test command locally before you push. Free plan can do this Visit official website ↗
- GitHub Copilot: Free plan: 2,000 completions a month plus limited chat and agent usage inside your editor, handy if you keep scripts in VS Code. Free plan can do this Visit official website ↗
- ChatGPT (incl. Work & Codex): Codex includes PR review; Free has limited Codex access. Free plan can do this Visit official website ↗
Reasons come from the capabilities, free-plan details and prices we've checked on official pages (separate from any dated trial above). Sources: claude.com/pricing · cursor.com/pricing · github.com/features/copilot/plans · chatgpt.com/pricing
Common mistakes
- Asking it to make failing tests pass without checking which side is wrong.
- Accepting tests that assert on mocks only.
- Not telling it your framework and style.
- Pasting proprietary code into a personal plan your employer hasn't approved.
Privacy tip
Check your employer's policy before sharing proprietary code. Remove keys and connection strings from config files.
Claude (incl. Claude Code) setting · Cursor setting · GitHub Copilot setting · Sources: privacy.claude.com/en/articles/12109829-how-do-i-change-my-model-improvement-privacy-settings · cursor.com/help/security-and-privacy/privacy · github.com/features/copilot/plans