How to Use Claude to Write Unit Tests That Catch Real Bugs

Claude writes unit tests quickly, and left alone it writes the wrong ones: a dozen green tests of the happy path, several of which assert that a mock returned what the mock was told to return. Fix it in three moves. Make it list the behaviours before it writes any code, make it hunt edge cases as a separate step, and make it prove each test fails when the code is broken. Then review the test names, not the test bodies.

Why are AI-written tests often useless?

A model asked to "write tests for this function" optimises for tests that pass. The cheapest way to pass is to restate the implementation. You get:

0:15 · loop 같은 AI, 같은 질문. 파일 하나로 전문가가 됩니다.
  • Mirror tests that recompute the expected value with the same logic as the code, so a bug in one is a bug in both.
  • Mock theatre: everything around the function is mocked, and the assertion checks the mock.
  • Happy-path padding: five tests with slightly different valid inputs and none with an empty list, a null, a timezone or a duplicate.
  • Snapshot dumps that freeze whatever the code does today, bugs included.

None of that is a Claude problem specifically. It is what happens when the instruction is "tests" instead of "behaviours".

What should you paste in before asking?

Four things change the output more than any phrasing: the function itself, its callers (so Claude sees how it is really used), one existing test file from your repo (so it copies your framework, naming and fixtures), and a one-line statement of what the function is supposed to do. The last one matters most. Without it, Claude infers intent from the code, and if the code is wrong, so is the inferred intent.

The skill this guide is built on
AI Unit Tests Generator Skill
AI Unit Tests Generator Skill
$19this skill, one-time vs $400+a QA contractor for a day

Turns Claude, Claude Code, ChatGPT or Gemini into a senior engineer who lists the scenarios first, writes one test per behaviour, and tells you plainly what is still uncovered. Works with Jest, Vitest, pytest and the framework already in your repo.

Get the Unit Tests Generator →

Step 1: behaviours before code

Here is a function and how it is called. [PASTE FUNCTION] [PASTE 1-3 CALL SITES] What it is supposed to do, in one line: [INTENT] Test framework and style: follow this existing test file exactly. [PASTE ONE EXISTING TEST FILE] Do not write any tests yet. List every distinct behaviour this function should have, one line each, written as a test name: "returns X when Y". Group them: normal cases, boundaries, invalid input, failure of dependencies. Mark any behaviour where the code and the intent seem to disagree with [MISMATCH].

Read the list. This is the two-minute review that matters: missing behaviours are obvious in a list of names and invisible in 300 lines of test code. Any [MISMATCH] line is a probable bug, found before a single test exists.

Step 2: hunt the edge cases on purpose

Now act as someone trying to break this function. For each input, list the values most likely to expose a bug: empty, single item, very large, negative, zero, null/None/undefined, duplicates, unicode, timezone and DST boundaries, floating point, concurrent calls, and a dependency that throws or times out. Keep only the ones that are realistic for how this function is called. Add them to the behaviour list as new test names. Do not write code yet.

Step 3: write tests that can fail

Write the tests for the agreed behaviour list. Rules: - One behaviour per test. The test name is the behaviour line. - Expected values are literals, worked out by hand. Never recompute them with the code under test. - Mock only I/O boundaries (network, disk, clock, random). Never mock the function under test or its pure helpers. - No snapshot tests. - For each test, add a one-line comment: "fails if <specific mutation of the code>". Then list any behaviour you could not test without changing the code, and why.

The "fails if" comment is the cheap version of mutation testing. If Claude cannot name a change to the code that would break a test, the test is not testing anything. For a real check, flip one comparison operator in the function and run the suite: something should go red. Python teams can automate this with mutmut, JavaScript teams with Stryker.

Need more than one specialist?

Buying the Unit Tests Generator gets you one file. The Library subscription loads every skill in the catalog through one connector, in Claude, ChatGPT Plus and up, Claude Code, Codex or Cursor, and keeps each skill updated when we improve it. $9 a month or $59 a year, with a free trial, no card needed.

Start the free trial → Why a subscription beats buying files

How do you use this in Claude Code?

In Claude Code the same three steps work better, because the agent can run the suite itself. Ask it to run the existing tests first and paste the result, then write the new tests, then run them again, then temporarily break the function and run them a third time. Ask for all three outputs in the reply. If you test in the browser, not just at unit level, the next skill covers it.

When unit tests are not enough
Elif - QA Automation Engineer AI Skill
Elif - QA Automation Engineer AI Skill
$19this skill, one-time vs $500+a QA automation contractor

Builds the rest of the test pyramid: Playwright UI automation with page objects, API tests, test data, waits that avoid flakiness, and CI with parallel runs.

Get Elif →

Where does Claude get unit tests wrong?

  • It invents APIs of your test library. Matchers and fixtures that do not exist in your version. Paste an existing test file and pin the version.
  • It cannot know the business rule. If the code rounds half-up and the requirement says half-even, Claude tests the code. The one-line intent is your defence.
  • It over-mocks. Say which boundaries may be mocked, every time.
  • It writes too many tests. Forty near-duplicates slow the suite and hide the three that matter. Ask for the smallest set that covers the behaviour list.
  • It makes flaky tests from time and randomness. Inject the clock and the seed; never let a test read the real time.

If you need test cases for manual QA or acceptance testing rather than code, try the free test case generator, or the AI Test Case Generator prompt for structured suites with preconditions and expected results.

How do you install a skill in Claude?

A KissMySkills skill is a ZIP file with a SKILL.md at its root. In the Claude desktop app, open Customize → Skills, add a skill, upload the ZIP and toggle it on. Skills need code execution, which lives under Settings → Capabilities; on Team and Enterprise plans an owner controls it. Anthropic documents the steps in its Skills help article, and our step-by-step install guide has screenshots.

In ChatGPT or Gemini, open the SKILL.md in a text editor and paste it into custom instructions: a Claude Project, a custom GPT or a Gem. With a Library subscription you skip the file entirely: connect KissMySkills once and ask for the specialist by name in the chat you already use.

In summary:

Behaviours first, edge cases second, tests last, and every test must be able to fail. The AI Unit Tests Generator ($19) holds those rules for you; Elif ($19) takes over when you need browser and API automation.

Skill · SKILL.md · Works with Claude & ChatGPTAI Unit Tests Generator Skill

AI Unit Tests Generator Skill

Scenarios first, one test per behaviour, literal expected values, and a plain list of what is still uncovered.

$19
Get the Unit Tests Generator →

Or get it with every other skill in Library: $9/month, free trial, no card needed.

KissMySkills is a marketplace of 2,500+ AI skills, agents, prompt packs & free tools for Claude, ChatGPT & any AI chat.

Frequently asked questions

Can Claude write unit tests for my code?

Yes, for Jest, Vitest, pytest and most other frameworks. Give it the function, its callers, one existing test file and a one-line statement of intent, and ask for a behaviour list before any code.

How do I stop AI tests from just testing mocks?

Tell it to mock only I/O boundaries such as network, disk, clock and randomness, never the function under test or its pure helpers, and to use literal expected values worked out by hand.

How do I know an AI-written test is useful?

Break the code on purpose, for example flip one comparison, and run the suite. A useful test goes red. Mutation testing tools such as mutmut for Python or Stryker for JavaScript automate this check.

Is Claude Code better than a chat for writing tests?

Usually yes, because it can run the suite before and after, and run it again with the code deliberately broken. In a chat window you have to run the tests yourself and paste the results back.

Should AI-generated tests replace manual test design?

No. The behaviour list is still a human review step, because only you know the business rule. AI speeds up writing and edge case discovery; it does not decide what correct means.

Related guides

이를 실천해 보세요

Or get every skill with Library See plans →

~/get-started

효과적으로 작동하는 Skills. 군더더기 없이.

스토어의 모든 스킬, prompt 팩 및 agent 둘러보기.

모든 스킬 찾아보기 →또는 무료 도구를 사용해 보세요