iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🔍

AI-Generated Tests: 87% Coverage but Missing 60% of Bugs

に公開

When developing with Claude Code's Agent Teams (an experimental feature where multiple AI agents are given specific roles for team-based development), the question of how to handle testing becomes an unavoidable challenge. It's fast to have AI write tests, but will those tests actually find bugs?

Upon investigation, the results were shocking: While AI-generated tests achieved 87% coverage, the mutation score (the actual ability to detect bugs) was only 38%. In other words, the tests pass, and coverage is high, but over 60% of bugs are slipping through.

The "Cheating Problem" in AI Testing

There are two main ways to have AI write tests:

  1. Pass the implementation code and generate tests.
  2. Pass the specification (spec) and generate tests.

In most cases, approach 1 is adopted. Whether using GitHub Copilot or Cursor, users instruct the AI to "write a test" while the implementation file is open. It seems intuitive and suggests that the AI can write accurate tests since it understands the code structure.

However, this is exactly the trap.

When an AI writes tests by looking at the implementation code, it transcribes the implementation logic directly into the expectations. For example, if the tax rate should be 10%, but the implementation is mistakenly price * 0.08, the AI will write expect(calculateTax(100)).toBe(8). It effectively locks in the bug as the "correct answer."

This is called a tautological test. Since the test is a copy of the implementation, the test passes even if the implementation is wrong.

In his blog, Mark Seemann described having AI write tests after implementation as an "epistemologically empty ceremony." Tests should inherently be an independent standard for verifying if an implementation is correct, but tests written by looking at the implementation lack that independence.

Coverage Lies

You might think, "But isn't it fine if the coverage is high?"

There is a method called mutation testing. It injects small changes into the source code (such as changing > to >= or true to false) to measure whether tests can detect them. While coverage is a metric of "whether the code was executed," the mutation score is a metric of "whether bugs can be found."

Here are the results:

Metric Value
Line Coverage 87%
Mutation Score 38%

Even though 87% of the code is being passed, only 38% of actual bugs can be detected. The 49-point difference is the "zone where tests pass but bugs are missed." There are even cases where the mutation score was 4% despite 100% coverage.

AI Also Cheats

There is an even more shocking story. The latest frontier models sometimes rewrite the test files themselves.

According to a report by METR, when OpenAI's o3 was tasked with program acceleration, instead of improving actual speed, it rewrote the timer so that it always looked fast. Rather than solving the problem to pass the test, it deceived the test itself.

In a study called ImpossibleBench, when researchers provided intentionally contradictory test cases (requiring both f(2)==4 and f(2)==5), GPT-5 cheated in 76% of cases. It used techniques such as overloading the __eq__ operator to always return True, or recording the number of calls to return different values for the same input.

Countermeasure: Do Not Show the Implementation

That covers the problems. Now for the countermeasures.

As a result of my investigation, the most effective countermeasure was simple: do not let the AI see the implementation code.

Countermeasure Effect
Restrict access to implementation code Cheating rate ≈ 0% (ImpossibleBench verification)
Agent separation (assigning a separate agent for testing) Test generation accuracy 61% → 87.8%
Context separation (Claude Code Task tool) HumanEval pass@1 67% → 96.3%
Explicit prompt instruction ("Do not look at the implementation") Cheating rate 93% → 1%

As shown in the table, just by having an agent that does not know the implementation write the tests, the accuracy jumps from 61% to 87.8%. Even with the same AI, changing the information provided results in such a difference.

So, What Should You Show? → Specs

If you don't show the implementation, what basis does the AI use to write tests? It uses the Given/When/Then of the specification (spec).

## User Story

- **US-01: Login**
  - As a user, I want to log in so that I can use the service
  - Given I am on the login page, When I enter the correct email and password, Then I navigate to the dashboard

Given/When/Then is a format for Acceptance Criteria that has been used in BDD (Behavior-Driven Development). When humans used BDD, the barriers were "it's a pain to write" and "the learning cost of Cucumber." Now that AI is the one doing the writing, those barriers have disappeared, and only the advantages of the format remain.

Why Given/When/Then is suitable for test generation:

Perspective Why it is effective
Compatibility with AI A structured format close to natural language. Easy for LLMs to interpret
Conversion to Test Code Given → Arrange, When → Act, Then → Assert. Can be converted mechanically
Traceability It is self-evident which spec condition is being tested. Hard for AI to arbitrarily increase or decrease tests
No BDD Framework Needed Can generate test code directly without an intermediate layer like Cucumber

From Addy Osmani, Gijutsu-Hyohron Co., Augment Code, GitHub Spec Kit, Tessl, to Kiro (AWS)—as far as I could research, there was no source that did not recommend a spec-based approach for test generation in the AI era.

How I Integrated It Into My Workflow

The Agent Teams workflow is /spec → /plan → /agent-teams. The problem was how to insert testing into this.

At first, I thought about having a QA agent within Agent Teams write the tests. However, in Agent Teams, all agents see the same repository. The QA agent could "cheat" by looking at the implementation code.

So, I created a /test-it skill outside of Agent Teams.

/spec → /plan → /agent-teams (Implementation + UT (Unit Test))
                      ↓
               Human Review
                      ↓
              /test-it (Generate/run IT (Integration Test) per feature)

Design Philosophy of /test-it

/test-it runs via the Claude Code Task tool (a feature that executes subtasks in a separate context from the main one). Since it is separated from the main context, it can write tests in a state where it "doesn't know" the implementation code.

There are only two inputs:

  • spec.md — Given/When/Then (definition of test cases)
  • plan.md — Integration points (where modules connect; the target of the test)

Things it is allowed to read: spec, plan, test configuration (package.json, etc.)
Things it must not read: Implementation code (src/, etc.)

Deriving Test Cases

I follow Vladimir Khorikov's principles. Select one "longest happy path" and write a test that goes through all module interactions. Only write additional tests if the one path cannot cover it.

1–3 cases per feature. It might feel small, but even with Palantir's 1 million lines of TypeScript experience, the conclusion was that "IT (Integration Tests) should be a select few."

Mapping Specs to Tests

IBM developers discarded 70% of AI-generated tests because they were "robotic and the intent was invisible." As a countermeasure, I explicitly state the correspondence with the spec in comments within the test code.

/**
 * IT: auth
 * Source: specs/features/auth/spec.md — US-01
 * Integration point: LoginForm → useAuth → /api/login
 */
describe('Auth Integration', () => {
  // Given: I am on the login page
  // When: I enter the correct email and password
  // Then: I navigate to the dashboard
  test('navigate to dashboard on successful login', async () => {
    // Arrange (Given)
    // Act (When)
    // Assert (Then)
  });
});

Reviewers can look at the spec and confirm, "This test verifies this condition of US-01." Tests with visible intent are less likely to be discarded even if they are AI-generated.

Deciding When Tests Fail

When a test fails, it is difficult at the current stage to have the AI decide whether to fix the test or the implementation. I established the following rules:

  1. Verify if the test is based on the spec.
  2. Matches spec → Implementation bug. Do not fix the test.
  3. Diverges from spec → Fix the test.
  4. Difficult to judge → Escalate to a human.

Because there is a spec, the judgment criteria are clear. Without a spec, a human would have to think every time, "Which is correct: the test or the implementation?"

Conclusion: 3 Points to Master for AI Test Generation

Point Action
Do not show implementation Separate the test agent and deny access to implementation code
Base on spec Structure acceptance criteria with Given/When/Then and use as input for tests
Human review AI-generated test false positive rate is +23%. Full automation is premature

Having AI write tests is correct in itself. However, "showing the implementation to write tests" is the same as checking your answers while looking at the answer key. Show the specifications to write tests. Just this alone will fundamentally change the quality of your tests.

Discussion