Course contentsShow
← Software Engineering
Lesson 602 of 1,93318. Test CoverageFree lesson

Mutation Testing

Discover how mutation testing measures test effectiveness by introducing code changes and checking if tests detect them.

Mutation Testing

What you'll learn: How mutation testing evaluates your test suite's ability to catch bugs by deliberately breaking your code.

The Core Idea

Traditional code coverage tells you which lines your tests execute, but it doesn't tell you if those tests actually verify anything meaningful. Mutation testing flips the script: it deliberately introduces small bugs (called "mutants") into your code and runs your test suite. If your tests fail, great—they caught the bug! If they pass, that's a problem—you have a blind spot.

Think of it like a security drill. Fire alarms test whether your smoke detectors work by simulating smoke. Mutation testing simulates bugs to test whether your tests actually detect problems.

How It Works

A mutation testing tool makes tiny changes to your source code:

  • Changes > to >=
  • Replaces + with -
  • Changes true to false
  • Removes a conditional check

Each change creates one "mutant." The tool runs your test suite against each mutant. A mutant is "killed" if at least one test fails (good—your tests caught it). A mutant "survives" if all tests pass (bad—you have insufficient or ineffective tests).

The mutation score is: (killed mutants / total mutants) × 100%

What It Reveals

Mutation testing exposes:

  • Tests that execute code but don't assert results
  • Missing edge case tests
  • Redundant or ineffective assertions
  • Gaps in branch and condition coverage

Unlike coverage metrics that measure execution, mutation testing measures detection effectiveness.

Key Takeaway: Mutation testing measures how well your tests catch bugs by introducing deliberate code changes—if your tests don't fail when the code breaks, they're not testing effectively enough.