Branch Testing is a white-box testing technique used to ensure that every possible branch (decision outcome) in a program is executed at least once during testing. It verifies all decision paths such as true and false conditions.
Tests all outcomes of decision statements (if, else, switch, loops)
Identifies logical errors in program flow
Improves code coverage and software reliability
Branch
A branch is a point in code where execution can follow different paths based on a condition.
Common sources of branches:
Conditional statements: if, else if, else
Switch/case blocks: each case is a distinct branch
Ternary operators: condition ? valueA : valueB
Short-circuit evaluation: A && B or A || B — if A is false, B is never evaluated
Exception handlers: try/catch/finally
Loop conditions: the decision to enter or skip a loop body
Even a single if statement with no else creates two branches: the branch where the condition is true (block executes) and the branch where it's false (block is skipped).
Branch Coverage
Branch Coverage measures how many branches in the code have been executed during testing.
Branch Coverage (%) = (Number of branches executed / Total branches) × 100
The goal is to achieve high coverage (ideally close to 100%) to ensure all decision paths are tested.
Example
Consider this simple function:
This function has six branches (two per if/elif/else condition — the true path and the false path at each decision). To achieve 100% branch coverage, your tests must collectively pass in a value that reaches each return statement.
A single test with score = 95 only covers one branch. You need at least three tests (score = 95, score = 75, score = 40) to cover all paths.
Process of Branch Testing
The following flowchart illustrates the step-by-step process involved in performing Branch Testing to ensure all decision branches are properly tested.