SupercovBlog

Guide

Statement vs branch coverage, with decision, line and path coverage

One function, four test sets, three tools: what statement, line, branch, path and MC/DC coverage each report.

Every statement ran. Not every branch. A card: Jest statements 7/7, 100%; Jest branches 4/6, no if was false; Go 100.0% of statements, no branch count.

Statement coverage counts the statements your tests ran. Branch coverage counts the outcomes of each decision: both the true and the false side of every if. One test can run every statement and still skip half the branches. In coverage tools, decision coverage means the same as branch coverage. Path coverage counts whole routes through a function, and none of the tools we measured reports it.

A control flow diagram of price(6000, true, true): total = cents, then if member and cents at least 5000, true to total minus 500, then if coupon, true to total halved, then return. Every statement ran. Both false edges, drawn dashed in red, were never taken.
One test runs every statement and skips both false branches.

The kinds at a glance

KindCountsReported by
LineLines with code that ranIstanbul, V8, JaCoCo, coverage.py
StatementStatements that ran; a line can hold severalIstanbul, V8, coverage.py, go test -cover
Branch (decision)Each outcome of each if, ?:, &&, switchIstanbul, V8, JaCoCo, coverage.py --branch
Condition / MC/DCEach condition inside a decision, shown to matterSupercov
PathEvery route through the functionNone of the tools we measured

We measured one function with Jest 30.5.2, coverage.py 7.17.2 and Go 1.27.4, then with Supercov 4.28.0:

function price(cents, member, coupon) {
  let total = cents;
  if (member && cents >= 5000) total = total - 500;
  if (coupon) {
    total = total / 2;
  }
  return total;
}

One test: every statement, half the branches

price(6000, true, true);
ToolStatementsBranches
Jest (Istanbul)7/74/6
coverage.py6/61/2 with --branch
go test -cover100%Not reported

Every statement ran, so statement and line coverage read 100%. Neither if was ever false. Jest counts six branches: two outcomes for each if, plus the two sides of &&. go test -cover reports statements only, so the missing outcomes do not show at all.

coverage.py counted 2 branches, not 4, because the first if and its body share a line, and it tracks branches as jumps between lines. Written over two lines, the same test showed 4 branches with 2 partial, and the report listed the missing jumps 3->5, 5->7.

A grid of four test sets against statements, branches, paths and MC/DC. One test: statements only, branches 4 of 6. Two tests: statements and branches, paths 2 of 4. Three tests: statements, branches and MC/DC, paths 3 of 4. Four tests: statements, branches and paths, with an MC/DC gap still left.
What each test set completed: every path still left one condition untested.

Two tests: 100% branches, half the paths

price(6000, true, true);
price(1000, false, false);

Branch coverage reached 6/6 in Jest and 100% in coverage.py: each if went both ways. The function has four paths, member discount with or without the coupon, and no discount with or without it. These two tests ran two of them. Reaching all four took four tests, and no tool’s numbers changed on the way: path coverage is not something they count.

Decision vs condition coverage

At 100% branch coverage, cents >= 5000 had never been false. The second test set member to false, so && stopped before reaching cents. Jest’s && branch counts whether each side ran, not whether it was ever false, so it still read 6/6.

 LINE  STATUS        SOURCE
    3  PARTIAL       member && cents >= 5000
       Unobserved: `cents >= 5000` was never false; needs `member` true,
       `cents >= 5000` false, decision false

Supercov reported this as an MC/DC gap. Adding price(1000, true, false) closed it with three tests. The four tests that ran every path did not: none of them had a member with a small order, so the gap was still there. See MC/DC vs branch coverage for how the two differ on larger decisions.

Which to use

Statement or line coverage tells you what never ran. Branch coverage tells you which decisions only ever went one way, and is the usual number to put a floor on. Condition and MC/DC coverage tell you which part of a compound condition no test changed. Path coverage doubles with every if in sequence, which is why the tools leave it out, and as the four tests above showed, every path still does not mean every condition.

What Supercov adds

npx supercov -- npm test
npx supercov runs latest file src/price.js

Supercov reports lines, statements, branches and MC/DC in one run, and names the test that covered each line. See Line, statement and branch coverage for how each tool counts lines.

More coverage guides