Definition
Branch Coverage
Branch coverage (or C_1, Decision Coverage) is a structural testing metric that measures the percentage of executed control branches.
Strict Definition: A branch is an outgoing edge from a basic block that has more than one outgoing edge (i.e., a conditional jump like
if,while,for). Unconditional jumps (e.g.,gotoor falling through to the next line) are edges but are not counted as “branches” for the denominator of this metric.
Concept
In the context of a Control Flow Graph, branch coverage requires visiting every conditional edge (True and False).
-
Relationship: Branch coverage subsumes statement coverage.
-
Logic: If you visit every branch (True/False), you must visit every node they lead to.
-
Exception: The reverse is not true (100% Statement \ne 100% Branch).
-
Industry Standard: This is the most widely used coverage criterion in industry (often visualized as “Diamonds” in tools like JaCoCo).
Example
Consider the absoluteSum method and its CFG:
graph TD Entry((Entry)) --> B1["BB<sub>1</sub>: sum = 0"] B1 --> b1{"BB<sub>2</sub>: b<sub>1</sub> = x < 0"} b1 -- "True" --> B3["BB<sub>3</sub>: x = -x"] b1 -- "False" --> b2{"BB<sub>4</sub>: b<sub>2</sub> = y < 0"} B3 --> b2 b2 -- "True" --> B5["BB<sub>5</sub>: y = -y"] b2 -- "False" --> B6["BB<sub>6</sub>: sum = x + y"] B5 --> B6 B6 --> Exit((Exit))
To achieve 100% Branch Coverage, we need to visit every Decision Branch.
Required Branches:
- (True):
- (False):
- (True):
- (False):
Note: The edge is an unconditional edge. While it is traversed, it does not count towards the “Total number of branches” in the formula.
Test Suite:
- Covers (Path: )
- Covers (Path: )
Result:
The test suite achieves 100% Branch Coverage. Unlike Statement Coverage, we explicitly tested the cases where the variables were not negative.
Logical Operators
Standard branch coverage treats a compound condition like
if (A && B)as a single decision. It requires the whole expression to be True and False, but does not necessarily test the individual components A and B independently. For that, Condition Coverage is needed.