Branch Coverage
What is Branch Coverage?
Branch coverage (also called decision coverage) is a white box testing technique that measures whether every possible branch (true and false outcome) from each decision point in the code has been executed at least once.
How It's Calculated
Branch coverage = (Number of branches executed / Total number of branches) × 100
Why It's Stronger than Statement Coverage
Branch coverage is a stricter criterion than statement coverage, since it requires both the true and false outcomes of every condition to be tested, catching logic errors that statement coverage alone might miss.
Worked Example: Full Statement Coverage, Half Branch Coverage
Consider a small function that gives members a discount:
def apply_discount(price, is_member):
if is_member:
price = price * 0.9
return price
Run one test, apply_discount(100, True). Every line executes, so statement coverage is 100%. But the decision if is_member has two outcomes, true and false, and only the true outcome was taken. Branch coverage is therefore 1 out of 2, which is 50%.
Now add apply_discount(100, False). The false outcome skips the discount line and goes straight to the return. Both branches are covered, giving 100% branch coverage. This second test is exactly the one that would catch a bug where non-members accidentally receive the discount.
Counting Branches in Loops, Switches and Compound Conditions
- if / else: two branches per decision, whether or not an
elseis written. - Loops: a
whileorforhas a branch that enters the body and one that skips or exits it. Test with zero iterations and at least one iteration. - switch / match: every case, including the default, is a separate branch.
- Compound conditions:
if age >= 18 and has_idcounts as one decision with two outcomes. Branch coverage is satisfied by one true and one false result, even ifhas_idwas never false. Condition coverage and MC/DC go deeper by testing each sub-condition separately, which is common in safety-critical software.
Measuring Branch Coverage With Tools
You rarely count branches by hand on a real project. Coverage tools instrument the code and report which outcomes ran. In Python, coverage.py measures branches when you pass the branch flag:
pip install coverage pytest
coverage run --branch -m pytest
coverage report -m # shows missed lines and partial branches
coverage html # writes a browsable report to htmlcov/
In the report, a line marked as a partial branch means one of its outcomes never ran. Java teams typically use JaCoCo, and JavaScript teams use Istanbul (nyc) or the coverage option built into their test runner. All of them highlight decisions where only one side was exercised.
Using the Number Wisely
Branch coverage tells you what code ran, not whether the results were checked. A test with no assertions can still raise coverage. Keep these points in mind:
- Read the uncovered branches first; they often point to missing error handling tests.
- Do not chase 100% on trivial code such as getters; focus on business rules and validation logic.
- Pair branch coverage with boundary value analysis so each branch is tested at its edges.
- Track coverage over time in the build pipeline so it does not quietly fall when new code arrives.
Practical checklist
- List every decision in the function.
- Write at least one test for the true outcome and one for the false outcome of each.
- Run the coverage tool with branch measurement switched on.
- Fix partial branches, then confirm each test has a meaningful assertion.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.