What is Branch coverage?

A white-box testing coverage criterion that requires both the true and false branches of every decision (such as an if statement) in a program to be executed at least once. It is also called decision coverage or C1, and it is stronger than statement coverage (C0), which requires every statement to be executed at least once.

Japanese name: 分岐網羅

Branch coverage appears in 2 practice questions across 1 topic, including Software Development and Project Management.

Field: Software Development and Project Management

How it differs from statement coverage

Statement coverage (C0) is achieved once every statement has been executed at least once. For if (a) { S1; } with no else, a single test case with a true executes S1 and achieves statement coverage, but the branch where a is false and execution simply moves on is never executed. Branch coverage (C1) also requires that false side, which makes it easier to find errors such as missing processing that is needed when a is false. Achieving branch coverage also achieves statement coverage.

Relation to condition coverage and compound conditions

When a decision is made of several conditions, as in 'A and B', condition coverage requires each individual condition to take both true and false. The two test cases (A = true, B = false) and (A = false, B = true) achieve condition coverage, but the decision is false in both, so branch coverage is not achieved. Therefore criteria such as decision/condition coverage, which satisfies both, and MC/DC (modified condition/decision coverage), which checks that each condition independently affects the outcome of the decision, are used. MC/DC is required at the highest safety level of DO-178C, the standard for airborne software, and is also highly recommended at ASIL D in ISO 26262, the functional safety standard for road vehicles.

Reading coverage figures

Coverage is expressed as the number of branches executed ÷ the total number of branches, and it can be measured with compiler or test tool features. Even 100% coverage does not find processing that is in the specification but was never implemented, or errors whose results depend on values. Coverage should be used as a tool to find the parts that tests have not exercised, combined with black-box testing. The same idea is used in hardware verification as RTL code coverage.

Try it: questions about Branch coverage

The truth values of a, b and c can be chosen independently of each other. For the code 'if (a) { S1; } if (b) { S2; } if (c) { S3; }', what is the minimum number of test cases needed to achieve branch coverage (executing both the true and false outcomes of every decision at least once)?

  1. A1
  2. B2
  3. C8
Show answer and explanation

Answer: B. 2

Each of the three decisions must be taken both ways, true and false. With the two test cases (a, b and c all true) and (all false), the true and false outcomes of each decision are each executed once, so the minimum is 2. A single test case (all true) executes all the statements S1 to S3 and so achieves statement coverage, but the false branches (the paths that do nothing and move on) are not executed. 8 is the number of all 2³ = 8 combinations of true and false.

FAQ

What do C0 and C1 mean?

They are abbreviations for coverage criteria: C0 is statement coverage (every statement executed at least once) and C1 is branch coverage (both the true and false sides of every branch executed at least once). Achieving C1 also achieves C0.

Should 100% branch coverage be the goal?

For safety-related software, standards or company rules may set targets for branch coverage or MC/DC. Otherwise, rather than insisting on 100%, it is important to check whether each unexecuted branch is really unnecessary or whether tests are missing. Code that runs only in abnormal situations needs to be tested, for example by fault injection.

Branch coverage in the explanations

Questions that mention Branch coverage (2)

  1. The truth values of a, b and c can be chosen independently of each other. For the code 'if (a) { S1; } if (b) { S2; } if (c) { S3; }', what is the minimum number of test cases needed to achieve branch coverage (executing both the true and false outcomes of every decision at least once)?
  2. For the decision 'A and B' (A and B are conditions whose truth values can be set independently), two test cases were run: (A = true, B = false) and (A = false, B = true). Which statement about coverage criteria is correct?

Books on Software Development and Project Management Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Short-circuit evaluation Agile development Black-box testing Boundary value analysis CI/CD (continuous integration and continuous delivery) Critical path method (CPM) Earned value management (EVM) Equivalence partitioning Scrum SLA (service level agreement)

Back to the glossary