#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
White Box Techniques

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 else is written.
  • Loops: a while or for has 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_id counts as one decision with two outcomes. Branch coverage is satisfied by one true and one false result, even if has_id was 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

  1. List every decision in the function.
  2. Write at least one test for the true outcome and one for the false outcome of each.
  3. Run the coverage tool with branch measurement switched on.
  4. 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.

Explore Course