Skip to content

Mutation Testing

In one line

Mutation testing changes your code in small ways (mutants) — if tests still pass, your tests have gaps.

In simple words

Mutation testing measures how good your tests are by deliberately breaking your code. It makes small changes (mutations) like changing + to -, true to false, > to >=, and runs your tests after each change.

If a mutation causes a test to fail — the mutation was "killed" (good, your test caught the bug). If all tests still pass — the mutation "survived" (bad, your tests would miss this bug).

Mutation score = killed mutations / total mutations. A high score means your tests are thorough.

Example mutations:

  • a + b → a - b (arithmetic operator)
  • return true → return false (boolean)
  • if (x > 0) → if (x >= 0) (boundary)
  • a && b → a || b (logical operator)

Mutation testing is computationally expensive (many test runs) but provides the most honest measure of test quality. Tools: Stryker (JS/TS), PIT (Java), mutmut (Python).

Example

javascript
// Original code
function isAdult(age) {
  return age >= 18;
}

// Test
test('isAdult returns true for 18', () => {
  expect(isAdult(18)).toBe(true);
});

// Mutations generated by Stryker:
// 1. age >= 18 → age > 18  → test FAILS (18 not adult) ✅ KILLED
// 2. age >= 18 → age <= 18 → test PASSES for 18 ❌ SURVIVED
//    (need more tests: expect(isAdult(17)).toBe(false))
// 3. return age >= 18 → return true → test PASSES ❌ SURVIVED
//    (need: expect(isAdult(10)).toBe(false))

// Mutation score: 1/3 = 33% — tests need improvement!

Stryker mutates your code and runs tests. Survived mutants reveal test gaps. A survived mutant means a real bug could go undetected.

Common confusions

  • Confused with: code coverage

    The difference: Code coverage measures which lines your tests execute — but 100% coverage doesn't mean tests are good (you can execute a line without checking its result). Mutation testing measures whether tests actually detect bugs — it changes the code and checks if tests notice. Coverage is necessary but not sufficient; mutation score is a stronger quality metric.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary