Software engineering / Code coverage

Code coverage

Run three small tests against one function and watch a coverage tool count the lines and branches they reach. Then see what a score of 100% leaves out.

Beginner7 steps~6 min
Line that ran — at least one test executed it
Line not run — no test has reached it yet
Branch — one way out of an if: yes or no
Test — calls the function, checks the answer
// THE SHORT VERSION

In 30 seconds

Code coverage measures how much of a program its tests actually run. Line coverage is the share of lines executed. Branch coverage is stricter: every if must have gone both ways.

It is good at finding code no test touches. It says nothing about whether the tests check the right things: code can run in a test that verifies nothing, and inputs nobody tried stay invisible.

So a low number is a clear warning, while a high one is not a guarantee. Aiming for exactly 100% often costs more than it returns.

Code coverage, step by step

STEPS01 / 07

Tools differ in detail: many count statements rather than lines, and some also count functions or paths. The effort curve in the last step is a sketch of the usual pattern, not measured data.

STEP 01 / 07 Which code do tests run?
THE MATH
coverage = lines run ÷ lines in total
  1. Step 1: Which code do tests run?. A coverage tool watches the code while the tests run and notes every line that executes. Coverage is the share that ran. Our example works out a shipping cost: 5 normally, 3 for members, free from 50 up. It has 6 lines that do something and two ifs.
  2. Step 2: One test: 5 of 6 lines. The first test calls shipping(20, true) and expects 3. The member line runs; the total is under 50, so the line that makes shipping free is skipped. Five of the six lines ran: line coverage is 83%. The tool points at the line no test has touched.
  3. Step 3: Second test: every line. A second test, shipping(60, true), expects 0. Now the free-shipping line runs too, so every line has executed at least once: 100% line coverage. It sounds complete, but one case has still never been tried.
  4. Step 4: Branches: 3 of 4. Each if can go two ways: yes or no. Branch coverage counts how many of those ways the tests took. The total check went both ways, but the member check only ever answered yes, since no test used a non-member. Three of four branches: 75%, despite 100% of lines.
  5. Step 5: Third test: every branch. A third test, shipping(20, false), expects 5: a non-member with a small order. The member check now answers no once, so all four branches have been taken. Branch coverage is the stricter measure: full branch coverage normally brings full line coverage with it, but not the other way round.
  6. Step 6: What the number hides. Coverage records that code ran, not that anyone checked the result: a test with no expect line scores the same. And it cannot see inputs you never tried. The rule changes at exactly 50, yet no test uses 50, so a slip from >= to > would go unnoticed at 100%.
  7. Step 7: Chasing 100%. Covering ordinary logic is cheap; the last few percent are rare error paths that take real effort to reach. And when the number becomes the goal, it is tempting to write tests that touch lines without checking them. Coverage works better as a map of what is untested than as a score to maximise.
VALUES SO FAR
lines6total
branches4total
lines····1 test
lines····2 tests
branches····2 tests
branches····3 tests
total 50····gap

Spotted a mistake on this page?