Course contentsShow
← Software Engineering
Lesson 587 of 1,93318. Test CoverageFree lesson

Branch Coverage

Measuring whether both true and false branches of conditionals are tested, catching untested decision paths.

Branch Coverage

What you'll learn: You'll discover how Branch Coverage ensures that every possible decision path in your code—both the "yes" and the "no" outcomes—gets exercised by your tests.

What Branch Coverage Measures

When your code contains conditional statements (if, else, switch, while), each creates a fork in the road. Branch Coverage measures whether your tests explore both paths at every fork. It's not enough to just execute the line with the if statement—you need to test when the condition is true and when it's false.

Think of it like a choose-your-own-adventure book. Line Coverage only checks if you read the page with the decision. Branch Coverage ensures you've tried both "turn left" and "turn right" choices.

Why Both Branches Matter

Imagine testing a function with an if statement. If your test only triggers the true condition, you have 100% Line Coverage but only 50% Branch Coverage. The false path—which might contain a critical bug, missing logic, or incorrect error handling—remains untested.

if (user.isAdmin) {
    grantAccess();
} else {
    denyAccess();
}

You need tests that exercise both user.isAdmin === true and user.isAdmin === false to achieve full Branch Coverage.

Catching Hidden Bugs

Branch Coverage reveals untested decision paths that Line Coverage misses. A conditional without an else block might look covered, but what happens when the condition is false? Branch Coverage forces you to verify that omitted path behaves correctly.

This metric is particularly valuable for security-critical code, permission checks, and error handling—places where an untested branch could cause serious problems in production.

Key Takeaway: Branch Coverage ensures every fork in your code's decision logic is tested in both directions, catching bugs hiding in untested conditional paths that Line Coverage overlooks.