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.
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
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 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.
- 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.
- 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.
- 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.
- 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.
- 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%.
- 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.
Try it yourself
Learn this first
Spotted a mistake on this page?