ISTQB Foundation/Ch.4 · Test Analysis and Design (34%) · Topic 4

White-Box Testing: Statement & Branch Coverage

Compute statement and branch coverage for a small function and explain why branch coverage is the stronger criterion.

White-box (structure-based) testing derives tests from the code's internal structure rather than its specification, and is typically applied at component or integration level, often by the developer who wrote the code. Statement coverage measures the percentage of executable statements exercised by a test suite: (statements executed) / (total statements) × 100.

Branch coverage measures the percentage of decision outcomes exercised — both the true and false side of every if, every case of a switch, both directions of a loop condition. Branch coverage is the stronger criterion: 100% branch coverage always implies 100% statement coverage, but the reverse is not true — a single test can execute every statement in a simple if block with no else while never exercising the false path at all.

This is exactly why a Playwright or unit-test suite that reports high statement coverage can still ship a bug hiding in an untested else branch: the metric that would have caught it — branch coverage — was never measured.

Real-world use case

A grading function's if (score >= 60) result = "Pass" line is fully statement-covered by one passing-score test — but the untested case (a failing score, which takes the implicit false branch and returns nothing) is where a real defect (should return "Fail", returns undefined) hides. Only a second test with a failing score, chosen specifically to hit the false branch, achieves branch coverage and catches it.

Example code

One test = 100% statement coverage, only 50% branch coverage
function grade(score: number): string {
  let result = "Fail";           // statement 1
  if (score >= 60) {
    result = "Pass";             // statement 2 — only runs on the TRUE branch
  }
  return result;                 // statement 3
}

// Test: grade(70) → "Pass"
//   Statements executed: 1, 2, 3 → 100% statement coverage
//   Branches executed:   only the TRUE side of "score >= 60" → 50% branch coverage
// A second test, grade(40), is required to exercise the FALSE branch and reach
// 100% branch coverage.

Exercise

Reach 100% branch coverage

For the function below, write the minimum set of test inputs needed to achieve 100% branch coverage, and state the statement coverage each individual test achieves on its own.

function shippingFee(subtotal: number, isMember: boolean): number {
  let fee = 5.99;
  if (subtotal >= 50) {
    fee = 0;
  }
  if (isMember) {
    fee = fee * 0.5;
  }
  return fee;
}

// Test 1 (inputs):                 Branches it exercises:
// Test 2 (inputs):                 Branches it exercises:
// Test 3 (inputs):                 Branches it exercises:
// Test 4 (inputs):                 Branches it exercises:

Read-only here — open the English track at /learn to write and edit this exercise.

Quiz (3)

1. A function has 10 executable statements and one `if` statement with no `else`. A single test executes all 10 statements by taking the `if`'s true branch. What is the branch coverage?

2. Why is 100% branch coverage described as a stronger criterion than 100% statement coverage?

3. Which test level is white-box testing most typically associated with?