Home › Coding › Write unit tests: how to do it with AI

Write unit tests: how to do it with AI

Last checked

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.

Best pick

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

  1. Pick one function or module and write down what it should do, including edge cases.
  2. Give the assistant the code, your test framework (pytest, Jest, JUnit…) and existing test style.
  3. Ask for tests that cover normal cases, edge cases and errors, each with a one-line reason.
  4. Run them. For each failure, decide whether the test or the code is wrong before letting it change either.
  5. Remove tests that only check mocks or duplicate others.
  6. 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 tests from behaviour
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]
Find what's untested
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 my tests
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

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

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

Related tasks

Report an error on this page