Using Mutation Testing to Catch Fake or Weak Tests Written by AI Coding Agents
Direct Answer
Mutation testing catches weak AI-generated tests by deliberately injecting small bugs (flipping > to >=, changing True to False, removing a return value) into your changed code and checking whether the test suite fails. If a test still passes after a bug is injected, that "mutant survived," proving the AI-written test only executed the line without verifying its behavior.
Saw Killed Any Mutants Lately? on Hacker News today, and it is one of the best defenses against low-effort AI PRs.
Why Line Coverage Lies on AI-Generated PRs
Coding agents are great at hitting high line coverage because simply calling a function with dummy data executes every line. Without checking assertions, a 98% coverage badge can still hide zero-value tests.
| Metric | What It Measures | Can an AI Agent Game It? |
|---|---|---|
| Line Coverage | Whether a line executed during tests | Yes, easily with smoke calls and weak mocks |
| Mutation Score | Whether tests fail when code logic is altered | No, requires real behavioral assertions |
Running Mutation Testing Only on Agent-Changed Files
Full-repo mutation testing is slow, so scope it strictly to the files modified in the PR diff:
bash# Run mutmut only on the module the coding agent just edited mutmut run --paths-to-mutate=BE/app/services/billing.py mutmut results
If an agent's new test suite leaves obvious surviving mutants on boundary checks (>= vs >) or error branches, bounce the PR back to the agent with the surviving mutant diff so it writes real assertions.
Frequently Asked Questions
Does mutation testing slow down every CI build too much?
Not if you scope it to files touched in the current PR (git diff --name-only) or run it as a nightly check on core billing, auth, and security modules.
Which tools work best for Python and TypeScript?
Use mutmut or cosmic-ray for Python, and StrykerJS for TypeScript and JavaScript repositories.