Claude Code for QA · 12 min read

Reviewing AI-Generated Tests: What a 95.6% Mutation Score Missed

Review AI-generated tests by checking where each expected value comes from, then reading the generator's notes: 100% coverage still let wrong behavior pass.

Line coverage, branch coverage and mutation score of the Claude-written semver tests, as generated and after review: 100%, 100% and 95.6% before, 100%, 100% and 96.5% after. The four strict-xfail tests that document the defects moved none of the three.
Line coverage, branch coverage and mutation score of the Claude-written semver tests, as generated and after review: 100%, 100% and 95.6% before, 100%, 100% and 96.5% after. The four strict-xfail tests that document the defects moved none of the three.

Testland asked Claude, in a fresh Claude Code session with no Testland plugins loaded, to write pytest unit tests for the Version class of python-semver 3.1.2. It returned 444 test cases, and all 444 passed. They ran 100% of the statements and branches in version.py, and mutmut, which plants small changes in the code to see whether any test notices, reported 571 of 597 mutants killed (95.6%). One test still pinned as correct a prerelease the SemVer spec forbids, "02", and no test failed on any of the three defects the agent named in its own reply. AI-generated tests tend to take their expected values from today's code, so a suite can pass, cover every branch and score high on mutation testing while still asserting wrong behavior: check where each expected value comes from before you trust those numbers.

This guide assumes a pytest suite you can run locally, pytest-cov for coverage, and Python 3.10 or later on Linux, macOS or WSL, because mutmut 3 needs fork. For xfail(strict=True), see pytest markers. No mutation-testing experience is needed.

Table of contents

Why generated tests treat today's code as correct

A generator's filters keep tests that build, pass and add coverage, so a passing generated test proves it runs against today's code, not that today's code is right. Meta's TestGen-LLM (FSE 2024 paper) "discards any test case that does not pass on first execution" and any test that does not "improve coverage". The tests that survive "protect the existing functionality of the system under test against future regressions." Coverage, the Google Testing Blog wrote in 2020, "does not guarantee that the covered lines or branches have been tested correctly, it just guarantees that they have been executed by a test."

A 2026 preprint by Charoosaei, Richter and Papadakis measured the cost. On 145 merged pull requests from SciPy, Qiskit and pandas, a Claude model wrote tests after seeing the faulty code and the diff, and "8%-17% of the generated tests are fault-enforcing, while only 2.4%-4.8% reveal faults." A fault-enforcing test passes on the buggy code and fails on the fixed code.

Invented APIs rarely reach review. TestGen-LLM credits its filters with "eliminating problems due to LLM hallucination". In this run ai-test-curator found 0 invented APIs.

Testland's run: Claude-written tests for python-semver's Version class

Each check saw a different slice of what the generated file got wrong.

The prompt, python-semver 3.1.2 and the measurement commands

The code under test is src/semver/version.py from semver 3.1.2 (PyPI, November 15, 2026, BSD-3-Clause): 889 lines, which coverage.py counts as 319 statements and 108 branches. Claude worked on November 22, 2026, in a folder that held only the package source, an empty tests/ folder and a pytest.ini, so every number below measures the generated file alone. Local paths are shown as <project> and <venv>:

Write pytest unit tests for the Version class in src/semver/version.py.

The project is at <project>. Put the tests in tests/test_version.py there. Run them with:

<venv>/bin/python -m pytest -q --rootdir <project> <project>/tests

Work only from the files in that folder (no web lookups). Don't change anything under src/. When the tests pass, reply with the number of tests and one line on what they cover.

It wrote one file: 1,127 lines, 122 test functions and 444 test cases once parametrization expands, all passing in 0.58s. The tools were Python 3.13.18, pytest 9.2.0, pytest-cov 7.1.0 and mutmut 3.8.1. The mutmut configuration, in setup.cfg, limits mutation to version.py and runs only the generated tests:

[mutmut]
source_paths=src/
only_mutate=
    src/semver/version.py
pytest_add_cli_args_test_selection=tests/

[tool:pytest]
pythonpath = src
testpaths = tests

Three commands produced the numbers:

pytest --cov=semver.version --cov-branch
mutmut run
mutmut results

mutmut results lists every mutant that was not killed.

What 100% branch coverage and a 95.6% mutation score missed

Every statement (319/319) and branch (108/108) ran, and mutmut killed 571 of 597 mutants, a 95.6% score, with 26 survivors. A mutant is a copy of the code with one small change, such as "< is changed to <=" (mutmut README). It is killed when "at least one test failed while this mutant was active" and survives when "all tests passed" (Stryker). The score is killed / total.

FindingCaught byDetail
Every statement and branch ranCoveragenothing flagged
4 survivors on 0 and empty inputsmutmuta 0 version part; an empty prerelease (two); an all-zero identifier
12 survivors that change only an error messagemutmut11 sit under match= patterns anchored only at the start of the message
10 equivalent or unreachable mutantsmutmut, sorted by handthe code changes, the behavior doesn't: decode("UTF-8") vs decode("utf-8")
11 of 34 pytest.raises with no match=, plus 3 other weak assertionsai-test-curator14 sites, medium
Equal versions hash differentlyai-test-curator; the generator's replyalready reported upstream as python-semver #515
A subclass's compare rejects a plain Versionai-test-curator; the generator's replyno upstream issue found
A "²" prerelease crashes comparethe generator's reply onlyfixed on python-semver's master branch, not released
A test pins the spec-invalid prerelease "02"ai-test-shallow-coverage-criticSemVer 2.0.0 item 9

Neither coverage nor the score moves when an expected value is wrong, so the review starts with expected values and the generator's notes, which no number shows, and ends with the numbers, the only part a tool can score.

Expected values that came from the code, not the SemVer spec

The costliest generated test takes its expected value from running the code, because it turns today's behavior, right or wrong, into a passing test that protects it. This excerpt is a method of the generated TestConstructor class (lines 57 to 61), so it runs only inside that file:

def test_prerelease_and_build_keep_leading_zeros(self):
    v = Version(1, 2, 3, "02", "007")
    assert v.prerelease == "02"
    assert v.build == "007"
    assert str(v) == "1.2.3-02+007"

SemVer 2.0.0 item 9 says of pre-release identifiers: "Numeric identifiers MUST NOT include leading zeroes." Item 10 sets no such rule for build metadata, so "007" is fine. The constructor's leniency looks deliberate, since the 3.1.2 source says "Keep constructor leading zeros." in _nat_cmp, the prerelease comparison, but only naming each value's source makes the spec deviation visible.

The pin sat in the 95.6% suite and, renamed test_current_behavior_accepts_leading_zero_prerelease_spec_deviation after review, in the 96.5% one. A test that asserts today's behavior kills mutants whether that behavior is right or wrong, so mutation testing can't find it; only ai-test-shallow-coverage-critic flagged it.

The source of a test's expected value is its test oracle. Charoosaei et al. write that "LLMs may infer assertions from what the current code does, rather than what it is intended to do", and advise that "reviewers should check them against the specification or the PR description". So name the source of every expected value (a spec clause, a documented contract, an issue, a language rule), and question each one whose only source is the code's output.

The review turned the hash defect into a strict xfail test, an expected failure whose XPASS results "fail the test suite" (pytest docs), as a method of a test class in the after-review file (lines 1266 to 1270):

@pytest.mark.xfail(strict=True, reason="__hash__ uses the raw prerelease, so '02' and '2' hash apart")
def test_equal_numeric_prereleases_hash_equal(self):
    a, b = Version(1, 0, 0, "02"), Version(1, 0, 0, "2")
    assert a == b
    assert hash(a) == hash(b)

Its oracle is Python's data model: "The only required property is that objects which compare equal have the same hash value". Where no spec exists, HICCUPPS-F lists other oracles.

What the generator said in its reply but did not test

The generator's hand-back message is a review input: this one named three defects. "I found three possible bugs in Version. The tests don't lock any of them in:"

  1. Equal versions can hash differently: Version(1,0,0,"02") == Version(1,0,0,"2") and Version(1,0,0,"") == Version(1,0,0), but the hashes differ.
  2. For a subclass, SubVersion(...).compare(Version(...)) raises TypeError, while the reverse returns 0.
  3. A "²" prerelease, which str.isdigit() accepts, makes compare raise ValueError from int().

Testland reproduced all three on 3.1.2. The hash and "²" cases need a direct constructor call, because Version.parse rejects those prereleases. The hash defect was already reported upstream (python-semver #515, November 16), and commit a4b7d33 fixed it and the "²" crash on the master branch on November 18. As of November 23, 2026, no release includes the fix, and no issue was found for the subclass case.

A reviewer never sees the chat behind someone else's PR, so ask for each suspected bug to arrive as a test of the correct behavior marked xfail(strict=True), with a reason and an issue link, or "issue to file" in the reason when none exists. The Claude agent that applied the review fixes wrote four, each with a reason (two hash cases, the subclass case and the "²" case). Three already pass on master, so they'll fail the suite as XPASS once a fixed release is installed: the signal to delete the marker.

Exception tests that check the type, or only the first words of the message

A pytest.raises passes for any exception of the right type unless match= pins the message, and pytest applies the pattern with re.search (raises() docstring), so a pattern covering only the start of a message passes when the end changes. The static review and the mutation run found different weak checks.

ai-test-curator listed the 11 of 34 pytest.raises calls with no match=. mutmut left 12 survivors that change only error text. Eleven of them sit under start-anchored patterns the generator wrote: one turns "hyphens." into "HYPHENS." under ^{token!r} is not a valid prerelease token\. It may only consist.

The review added match= to the 11 bare calls. Nine raise Python's own messages, which mutmut doesn't mutate. One, on Version.is_valid(1), got a pattern anchored at both ends and killed parse mutant 11; the last checks a message the file already pinned elsewhere. The start-anchored patterns stayed, and 11 message mutants still survive.

Anchor the whole message, as the generated test_negative_part_raises already did: match=rf"^'{part}' is negative\. A version can only be positive\.$". Pin text only where callers read the message as part of the contract; otherwise note the survivors and move on.

Zero and empty inputs: the gaps mutmut found in a fully covered suite

The suite exercised every input partition the static critic scores, yet four survivors sat on 0 and empty inputs, and only the mutation run pointed at those values. ai-test-shallow-coverage-critic scored 17 entry points with 0 SHALLOW on any axis.

mutmut show "semver.version.xǁVersionǁ__getitem____mutmut_46" prints the first. The name is the module, class and method, then the mutant's number:

# semver.version.xǁVersionǁ__getitem____mutmut_46: survived
--- src/semver/version.py
+++ src/semver/version.py
@@ -30,7 +30,7 @@
         raise IndexError("Version index cannot be negative")
 
     part = tuple(
-        filter(lambda p: p is not None, cast(Iterable, self.to_tuple()[index]))
+        filter(None, cast(Iterable, self.to_tuple()[index]))
     )
 
     if len(part) == 1:

Version(0, 1, 2)[0:2] returns (0, 1). Under the mutant, filter(None, ...) drops the 0 as well, so the method would return 1. Every indexing test in the generated file used numeric parts 1, 2 and 3. The other three are _nat_cmp fallbacks: one for an all-zero identifier and two for an empty prerelease. Three new test classes killed all four.

These are boundary values, which the ISTQB CTFL v4.0.1 syllabus tests by "exercising the boundaries of equivalence partitions"; for a version part, the boundary is 0. In Shi et al., a 2026 study of four LLMs on Java, "coverage of null, negative, and empty arguments reaches only 34.8%, 23.8%, and 38.8%".

Sort each survivor as message-only, missing boundary (add the case) or equivalent (note it; 10 here). The mutant-survival-triage skill does that sorting and drafts the test that kills each real gap.

What ai-test-curator and ai-test-shallow-coverage-critic flagged in the semver tests

The two agents in Testland's qa-ai-assisted plugin covered the static part of the review. ai-test-curator flags hallucinated APIs and weak assertions, among other issues, "and refuses to mark tests ready while a high-confidence issue remains". ai-test-shallow-coverage-critic checks each public entry point for partitions, boundary values and error paths.

The curator's verdict was "Ready to merge with recommended fixes.", with 0 high, 17 medium and 31 low sites. It flagged the hash and subclass defects as medium coverage gaps. Its method was "each expected value hand-traced against the source", which confirms what version.py does and leaves the spec check with the reviewer.

The critic flagged the pin: "Whether the constructor should reject these is a spec decision for a human, not a coverage gap." Its definition places it ahead of a mutation run: "this audit is the cheap pre-check that explains which input classes are missing."

A second Claude agent then applied the medium findings and the four boundary gaps. The new file still pins "00" and "01" prereleases as current behavior.

MeasureGenerated suiteAfter the review fixes
Test cases444487 (483 passed, 4 xfailed)
Statement coverage100%100%
Branch coverage100%100%
Mutation score95.6% (571/597)96.5% (576/597)

The four strict-xfail tests that document the defects moved neither number. The five extra kills (571 to 576) came from the boundary tests and one message mutant. Related posts are in the Claude Code for QA hub.

Questions about reviewing AI-generated unit tests

Should you merge AI-generated tests that pass and raise coverage?

Not on those two signals. TestGen-LLM keeps tests that pass and add coverage so that they "protect the existing functionality", and Charoosaei et al. warn: "Developers should therefore not treat a passing generated test as evidence of correctness". Check where each expected value comes from first.

Does mutation testing catch a test that asserts wrong behavior?

No. A test that asserts today's behavior kills mutants whether that behavior is right or wrong. In this run the "02" pin, a deviation from SemVer item 9, sat in a suite that scored 95.6% and was still there at 96.5% after review.

Can an LLM review AI-generated tests for wrong expected values?

Partly. Here ai-test-shallow-coverage-critic flagged the "02" deviation for a human, while ai-test-curator traced expected values against version.py, not the spec. A 2024 preprint by Konstantinou, Degiovanni and Papadakis (Java, a 2023-era OpenAI model) found that "LLMs are more effective at generating test oracles rather than classifying them." Keep the spec check with a person.

Does this review order work for Jest and JUnit tests?

Yes. The studies behind it cover Java and Python, and the checks don't depend on the language. For the mutation step, Stryker "has support for JavaScript & TypeScript, C# and Scala", and PIT covers "Java and the jvm". For E2E tests, see Playwright's healer agent in CI.

A review order for the next AI-written test file

  1. Name the source of each expected value, question values whose only source is the code's output, and rename tests that pin current behavior.
  2. Read the generator's hand-back, or ask for it, and get each suspected bug into the PR as a strict-xfail test with a reason and an issue link or "issue to file".
  3. Run pytest --cov=<module> --cov-branch, mutmut run and mutmut results, then mutmut show each survivor: anchor the full message, add the missing 0 or empty case, or note an equivalent mutant.

Start with mutmut run on the module the last AI-written PR touched; the mutmut 3.8.1 README covers installation and the setup.cfg keys.

Start with qa-starter.

10 plugins every tester needs on any stack: test planning, test data, environments, reporting, flaky tests, bug reports and exploratory testing.

/plugin install qa-starter@testland-qa