All posts

AI Engineering

Check AI-Written Tests with a Small Mutation-Testing Lab

Passing tests can still accept broken behavior. Try five deliberate faults in a synthetic line-deduplication helper, inspect missed assertions, and separate survivors from equivalent implementations.

Published and maintained by Sambro · Report a correction

8 min read Mutation testingAI codingTest designNode.js

Would the tests notice deliberately wrong behavior?

Suppose an AI writes a function and its tests, and everything passes. The assertions might capture the intended behavior. They might also share the implementation's mistaken assumptions. Add a concrete question: would these tests fail if the function sorted the output or silently discarded blank lines?

This lab puts five deliberate faults into a small line-deduplication helper and compares two suites. The code and tests were authored as synthetic teaching material. We did not measure a particular model's generated tests or benchmark its performance. Keep these variants in isolated files, away from production code.

Apply an established technique to AI coding work

Goran Petrovic wrote Google Testing Blog's Mutation Testing on April 12, 2021. It explains testing tests by injecting faults, while warning that unproductive mutations can encourage brittle assertions. Primary source: testing.googleblog.com — mutation-testing.html

Anthropic's Demystifying evals for AI agents was published January 9, 2026, by Mikaela Grace, Jeremy Hadfield, Rodrigo Olivares and Jiri De Jonghe. It emphasizes clear tasks, stable environments and thorough outcome tests for coding agents. It does not establish that mutation testing improves AI-written tests specifically. Primary source: www.anthropic.com — demystifying-evals-for-ai-agents

Neither source is a new announcement this week. This November 11, 2026 exercise adapts established ideas into a small review step. Its results are not a reproduction of a vendor benchmark.

Write the contract before introducing faults

The teaching helper accepts strings, normalizes CRLF to LF, compares lines exactly and preserves their first-occurrence order. Case and spaces remain significant; output is joined with LF. preserveBlank defaults to true, retaining one empty line. When false, only zero-length lines are removed. A line containing one space is not empty.

Empty input returns an empty string. A lone CR is data, not a separator. A final LF can create an empty entry after splitting. These simplified rules are not the full contract of Sambro's deployed tool. This is not a copied production implementation or its regression suite.

Expected values come directly from the contract. For input b, a, b, the expected order is b, a. That catches an implementation that sorts the output into a plausible-looking a, b. Computing expectations with the function under test would copy its mistake into the answer key.

Order-preservation contract: input and expected output
input:    "b\na\nb"
expected: "b\na"

Run the dependency-free harness

Use Node.js 22 or later. Save all six files in one directory: implementations mutation check — variants.mjs , contract cases mutation check — suites.mjs , harness mutation check — harness.mjs , runner mutation check — demo.mjs , harness tests mutation check — harness.test.mjs , and recorded output mutation check — results.json . Read the code before running the commands below. No package installation or API key is required.

The weak suite has one ordinary duplicate case. The improved suite has eight cases covering order, case, blanks, CRLF, empty input and lone CR. Both suites must pass on the reference implementation before mutant results are interpreted. If either baseline fails, the harness returns valid:false and skips the mutants.

Every variant is a fixed, short, terminating synchronous function. This harness is not a sandbox for arbitrary generated code, potential infinite loops or external file execution. It uses only the included cases and makes no network or model calls.

Run with all six files in one directory
node --test harness.test.mjs
node demo.mjs

Inspect which faults the assertions missed

The reference passed both suites. The weak suite detected the return-input variant and missed the other four. The improved suite detected all five deliberately incorrect variants. In the matrix, killed means a returned value differed from a contract expectation.

That proportion applies only to these five hand-selected faults. It is not a project-wide defect detection rate or an AI model score. The useful result is the case that detects each fault: first-occurrence-order exposes sorting, while case-sensitive exposes lowercasing. Connect each failure to a behavior the user needs.

The recorded run used Node v24.22.1. Its detailed output is in results.json, including the Node version in runtime. Rerunning on another version can change that metadata. Write a fresh result to a separate file when comparing runs rather than overwriting the recorded one.

Executed fault matrix and separately classified equivalent control
variant                    weak       improved
sort-output                survived   killed
lowercase                  survived   killed
drop-blank                 survived   killed
no-crlf-normalization      survived   killed
return-input               killed     killed

equivalent-indexof         pass       pass (control, excluded)

A surviving change is not automatically a defect

The equivalent-indexof control chooses first occurrences with indexOf instead of Set and passes both suites. Its equivalence for the stated string-input contract is justified by inspecting the code, not inferred solely from a finite passing suite. It is excluded from the five-mutant denominator.

For an actual survivor, first decide whether the changed behavior violates a requirement. If it does, add a case that exposes the user-visible difference. If it is equivalent or outside the contract, record that reason separately. Chasing a 100% score by pinning irrelevant implementation details can make refactoring harder.

The harness labels exceptions and non-string results execution-error; malformed test inputs become harness-error. This exercise does not count either as killed. It has no process isolation or timeout enforcement. Automatically generated variants require separate isolation and time limits before they can be safely executed.

Ask AI for contract-based cases

Have a person settle the contract and unacceptable behavior first. Ask the AI for inputs and exact expectations from that contract rather than a paraphrase of the implementation. Review those expectations before trying the reference and deliberately broken variants. Two calls to the same model do not establish independent review.

In a real repository, start with one changed function and its relevant cases, then rerun the existing checks after adding tests. Never ship the intentionally broken variants in the production implementation. A survivor can reveal a code defect, an ambiguous contract or a weak test; decide which one needs changing.

Example request for test proposals
Contract: compare string lines exactly and preserve first-occurrence order.
Propose order, case, line-ending and blank-line cases without copying the implementation.
For each, give input, exact expected output and the rule it protects.
Do not derive expectations by running the implementation; flag ambiguous requirements.

Keep the teaching helper separate from the live tool

For ordinary list cleanup, use the English Duplicate Line Remover: Duplicate line remover . The deployed tool includes trimming, case-insensitive matching and blank-line options, so inspect its instructions rather than assuming the same settings as this lab. All example strings are invented, not real rosters.

The exercise demonstrates detection of specific returned-value faults. It does not test security, asynchronous behavior, browser events, performance or real user data. For the next application, choose one important contract that could otherwise be missed instead of optimizing the number of tests.

Back to Sambro Blog