Definition
Branch coverage is a coverage metric §10.18 whose elements are the possible outcomes of a program’s decisions. A decision is a point where the program chooses which code runs next, such as the condition of an if or a loop, or a switch. An if has two outcomes, true and false; a switch has one per distinct target. Branch coverage is the fraction of these outcomes that some test produced. Many texts call it decision coverage. Some tools count each condition inside a compound decision separately.
The idea
A message handler should accept a message only if its length is valid and its tag is valid: if (len_ok && tag_ok) accept();. The programmer typed ||, the or operator, instead of &&, the and operator, so a message with a valid length and a forged tag is accepted. Two tests check the handler. Test 1 has both conditions true and expects acceptance; test 2 has both false and expects rejection. With ||, test 1 accepts and test 2 rejects, so both tests pass. The decision came out true once and false once, branch coverage is 100%, and the bug survives.
The two tests changed both conditions at once, so neither condition was shown to matter on its own. The stronger criterion MC/DC (modified condition/decision coverage) requires, for each condition, a pair of tests in which only that condition changes and the outcome changes with it: here, one test with only len_ok false and one with only tag_ok false. Both expect rejection, and the faulty || version accepts both, so each of them finds the bug.
How it works
Branch coverage is measured in three steps.
List the decisions and their outcomes. An
ifhas two outcomes. A loop condition has two: entering the body and leaving the loop. Aswitchhas one outcome for each distinct case target, plus the default, or, when no default is written, the outcome in which no case matches.Instrument. The compiler adds a counter to each outcome.
Count. Branch coverage is the number of outcomes with a nonzero count, divided by the number of outcomes.
A condition is a Boolean expression that contains none of the Boolean operators && (and), || (or) and ! (not), such as len_ok; a decision is built from one or more conditions. The criteria for compound decisions, from weaker to stronger, are these.
Decision coverage, the same as branch coverage: each decision takes each outcome.
Condition coverage: each condition takes the values true and false. This does not imply decision coverage. For
a & b, where C’s operator&evaluates both sides, the tests (atrue,bfalse) and (afalse,btrue) give each condition both values, yet the decision is false both times.Condition/decision coverage: both of the above.
MC/DC: condition/decision coverage, every entry and exit point of the program invoked, and each condition shown to independently affect the outcome of its decision. A pair of tests shows this when it changes only that condition, holds every other condition fixed and changes the outcome. A variant called masking MC/DC also lets other conditions change, provided they cannot affect the outcome in those two tests.
Multiple-condition coverage: every combination of condition values, combinations for independent conditions.
The aviation software standard DO-178C (Software Considerations in Airborne Systems and Equipment Certification) requires MC/DC for software of its Level A, software whose failure could cause or contribute to a catastrophic failure condition for the aircraft. It requires decision coverage for Level B and statement coverage for Level C.
Short-circuit evaluation changes what can be observed. In C, a && b evaluates b only when a is true, and a || b evaluates b only when a is false, so the compiler emits a separate jump for each condition. Tools therefore differ in what they call a branch. Clang’s §12.17 source-based coverage counts the true and false outcomes of each condition, four outcomes for a && b; a tool that counts decision outcomes counts two. The same tests can receive different branch-coverage figures from different tools.
Worked example
Counting. A function contains an if (2 outcomes), a while loop (2 outcomes) and a switch with three case labels and a default (4 outcomes), so it has outcomes. If the tests never make the if condition false and never reach the default, they produce outcomes, and branch coverage is , or 75%.
The compound decision. Take if (len_ok && tag_ok) with test t1 = (len_ok true, tag_ok true) and test t2 = (both false).
Decision outcomes: t1 gives true and t2 gives false, so decision coverage is .
Condition outcomes, counted the way Clang counts them:
len_okis true in t1 and false in t2, 2 of 2.tag_okis evaluated only in t1, where it is true, because in t2 the falselen_okends the evaluation first; that is 1 of 2. In total .MC/DC depends on the rule for independence. Under the unique-cause rule on the supplied inputs, both inputs changed, so this pair shows neither input changing alone. Clang’s short-circuit measurement sees a different pair of traces: (true, true) and (false, unevaluated). It credits the first condition because the second was skipped in the false trace. It cannot credit the second condition until a test actually evaluates it as false.
Now add t3 = (len_ok false, tag_ok true) and t4 = (len_ok true, tag_ok false); both expect false. The pair t1, t3 differs only in len_ok, and the outcome changes from true to false. The pair t1, t4 differs only in tag_ok, and the outcome changes in the same way. The three tests t1, t3 and t4 achieve MC/DC, which is tests for conditions; t2 is no longer needed. Every combination would take tests. Against the faulty || version, t3 and t4 both return true where false is expected, so the bug is found. The gap widens quickly: for independent conditions, unique-cause MC/DC needs at least 11 tests, every combination .
Stated precisely
Let be the set of decisions in the measured code, and for each decision let be its set of possible outcomes. Then where is the number of elements of a set .
DO-178C defines MC/DC as four requirements that a set of tests must meet together: every point of entry and exit in the program has been invoked at least once; every decision has taken all possible outcomes at least once; every condition in a decision has taken all possible outcomes at least once; and each condition in a decision has been shown to independently affect that decision’s outcome. A condition is shown to independently affect the outcome by varying just that condition while holding fixed all other conditions, or all other conditions that could affect the outcome. When unique-cause MC/DC can be achieved for a decision with conditions, it needs at least tests.
Limits and common mistakes
Branch coverage counts the outcomes of decisions, not the effect of each condition, as the handler with || shows. Nor is it path coverage: decisions in sequence combine, and covering each decision’s outcomes separately can miss the one combination that fails.
Even every combination of every decision shows only that each combination ran. It does not show that the code inside a taken branch computed the right result, avoided undefined behavior §4.12 or met its deadline. A test oracle §10.5 has to check those.
Compilers and tools disagree on what a branch is. In compiled code, short-circuit operators and the conditional operator ?: become jumps, and optimization can remove jumps by turning them into conditional-move instructions. Compare figures only from the same tool and the same build.
A condition that can never independently affect its decision makes MC/DC impossible for that condition. In (a && b) || (a && !b), the outcome always equals a, so no pair of tests can show an effect of b. That failure is useful: it reveals a redundant or mistaken condition.
A defensive branch that can never be taken, such as a check for an error the hardware never reports, keeps the figure below 100%. The right response is to list it as an exclusion with its reason, not to delete the check.
Questions answered
Does each condition inside a compound branch independently affect the outcome?
Not necessarily, and branch coverage cannot tell. Branch coverage requires only that each decision as a whole came out true and false at least once. For if (len_ok && tag_ok), the two tests (both conditions true) and (both false) reach 100% branch coverage but do not test what happens when the second condition is evaluated and false. A faulty version that uses or (||) instead of and (&&) passes both tests.
That each condition independently affects the outcome is what MC/DC (modified condition/decision coverage) demands. For each condition, MC/DC needs a pair of tests that changes only that condition, holds the other conditions fixed, and changes the outcome of the decision; a masking variant also lets other conditions change where they cannot affect the outcome. For the example, the tests (true, true), (false, true) and (true, false) do this, tests for conditions, and they catch the faulty or. When a pair is infeasible, look at which inputs the program can actually receive. If two condition occurrences read the same Boolean, a test cannot change one occurrence while holding the other fixed. That is a constraint on the test pair. Deciding whether the code is redundant takes a separate argument.
Related entries: Coverage metric (§10.18), Boundary-value testing (§10.31), Coverage versus correctness (§10.40), Edge coverage (§10.19), Test oracle (§10.5).