Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Structural coverage testing

Structural (white-box) coverage testing measures how thoroughly program structure (statements, branches, conditions, paths) is exercised by tests, using control-flow graphs and metrics like statement coverage, branch coverage and cyclomatic complexity.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Structural coverage testing is used by engineers when they design test suites for code units to prove that internal program structure is exercised. It helps find untested code, guides test-case design (especially for unit and integration testing), and provides measurable criteria (statement, branch, condition, path coverage) used in test reports and quality audits.

Key ideas

Start from first principles and definitions.

  • A control flow graph (CFG) is a directed graph representing a program (or a function) where each node is a basic block (a straight-line sequence of executable statements with one entry and one exit) and each edge represents possible transfer of control between blocks. When asked to draw a CFG in an exam: draw boxes for basic blocks, arrows for control transfers, label each node (B1, B2, …) and label edges when helpful (true/false for conditional edges). Include an explicit entry and exit node.

  • Statement coverage (also called line coverage) is the fraction of executable statements executed by a test suite. Formal definition: statement coverage = (number of executed statements) / (total number of executable statements).

  • A decision (or branch point) is a program point where control can go to two or more different successors (for example an if or switch). A branch is a single outgoing edge from such a decision (e.g., the true-branch or false-branch). Branch coverage (a.k.a. decision coverage) is the fraction of branches (edges) in the CFG that have been taken at least once by the test suite.

  • A condition is a boolean sub-expression inside a decision (for example A, B, C inside a compound boolean A && (B || C)). Condition coverage requires each simple condition to take both true and false values at least once across the test set.

  • Multiple Condition Coverage (MCC) demands that every possible combination of truth values of the atomic conditions be tested (this grows exponentially with the number of conditions).

  • Modified Condition/Decision Coverage (MC/DC): For each atomic condition, there must be a pair of test cases where (1) the atomic condition takes different values, (2) all other atomic conditions keep the same values in both cases, and (3) the decision outcome is different in the two cases. This ensures each condition independently affects the decision outcome. Exact statement to memorize: "For each condition in a decision, show at least one pair of tests in which the condition alone (with all other conditions held fixed) causes the decision to toggle." State the assumption that conditions are independent logical expressions; when conditions are logically redundant MC/DC may not be achievable without refactoring.

  • Path coverage is the fraction of feasible paths through the CFG executed by tests. Because loops induce infinitely many distinct paths, in practice restrict to acyclic/simple paths or bound loop iterations. Always count only feasible paths (some syntactic paths are infeasible due to contradictory conditions).

  • Cyclomatic complexity V(G) is a metric from graph theory that gives a count of independent paths through the CFG. Exact formula (for a structured, connected CFG) is: V(G) = E − N + 2P where E is the number of edges, N the number of nodes, and P the number of connected components (usually 1 for a single function). For a single connected program graph, a useful corollary is: V(G) = D + 1 where D is the number of binary decisions (if, while, for). For a k-way case/switch, add k − 1 to D (each additional outcome beyond the first increases complexity by one). When asked for a derivation in an exam: cite the cyclomatic number from graph theory or sketch a rank-nullity argument on the incidence matrix; do not assume planarity. For a connected CFG (P = 1), V(G) = E − N + 2.

  • Relationship and assumptions: 100% statement coverage does not imply 100% branch coverage. 100% branch coverage implies 100% statement coverage for structured code (assume no unreachable statements). Also, 100% condition coverage does not imply 100% branch coverage (and vice versa), especially with short-circuit evaluation. Cyclomatic complexity gives the number of independent paths, not the total number of feasible paths; use V(G) as the size of a basis set of paths for structured path testing.

Uncertainties / where to check: details about counting decisions in some exotic language constructs (e.g., switch with fall-through, short-circuit operators) can vary by lab or paper; check your university's marking scheme for exact counting rules.

Formulas

StatementCoverage(%) = (ExecutedStatements / TotalExecutableStatements) * 100 — ExecutedStatements and TotalExecutableStatements are counts (dimensionless), applies when statements are unambiguously counted.

BranchCoverage(%) = (CoveredBranches / TotalBranches) * 100 — Branches are edges from decisions; applies to CFGs with clearly identified branching edges.

PathCoverage(%) = (CoveredFeasiblePaths / TotalFeasiblePaths) * 100 — Often TotalFeasiblePaths is undefined when loops produce infinite paths; restrict to simple/acyclic paths or bound loop iterations.

V(G) = E - N + 2P — E = number of edges in CFG, N = number of nodes, P = number of connected components (usually 1). Unitless. Applies to general CFGs; for a single connected CFG, P = 1.

Alternative: V(G) = D + 1 — D = number of binary decisions (when the CFG is connected and each binary decision increases complexity by one); for a k-way case/switch, add k − 1 to D. Unitless.

Remember syntax to state: "Achieves 100% X coverage" means every element of type X (statements, branches, conditions) is exercised.

Worked examples

Example 1 — small program: compute statement and branch coverage and cyclomatic complexity. Code (pseudo): 1: a = input() // S1 2: if (a > 0) // D1 3: b = 1 // S2 4: else 5: b = -1 // S3 6: if (a % 2 == 0) // D2 7: c = 0 // S4 8: print(b, c) // S5

Executable statements: S1, S2, S3, S4, S5 (total = 5). Decisions: D1 (true/false), D2 (true/false) → total branches = 4.

Test suite: T1: a = 2 (positive even)

  • Executes S1, D1 true→S2, D2 true→S4, S5. Covered statements: {S1,S2,S4,S5}; branches covered: D1-true, D2-true.

T2: a = -3 (negative odd)

  • Executes S1, D1 false→S3, D2 false (not even)→skip S4, S5. Covered statements: {S1,S3,S5}; branches covered: D1-false, D2-false.

Union of tests covers all statements {S1,S2,S3,S4,S5} => Statement coverage = (5/5)*100 = 100%. Union of branches covered: {D1-true, D1-false, D2-true, D2-false} => Branch coverage = (4/4)*100 = 100%.

CFG counting for cyclomatic complexity (draw nodes for entry, S1, D1, S2, S3, D2, S4, S5, exit). Suppose N = 9 nodes, E = 10 edges, P = 1 → V(G) = E - N + 2P = 10 - 9 + 2 = 3 → V(G) = 3. Interpretation: there are 3 independent paths. Note: total feasible acyclic paths = 4 (D1×D2 combinations), but V(G) = 3 gives the number of independent paths forming a basis for path testing. This is a common exam point: explain difference between number of feasible paths (combinatorial) and cyclomatic complexity (basis size).

Example 2 — MC/DC construction for decision: D: A && (B || C) Truth table rows (A,B,C) and decision result D:

  1. (T,F,F) => A && (F || F) = F
  2. (T,F,T) => T && (F || T) = T
  3. (F,F,T) => F && (F || T) = F
  4. (T,T,F) => T && (T || F) = T

To satisfy MC/DC, for each atomic condition find a pair within the test set that differs only in that condition and causes the decision to toggle. A correct minimal test set for D = A && (B || C) is rows 1, 2, 3, and 4:

  • For A: compare rows 2 (T,F,T => D=T) and 3 (F,F,T => D=F): only A differs and D toggles.
  • For B: compare rows 4 (T,T,F => D=T) and 1 (T,F,F => D=F): only B differs and D toggles.
  • For C: compare rows 2 (T,F,T => D=T) and 1 (T,F,F => D=F): only C differs and D toggles. Show these pairs in the exam with explicit explanation for each condition.

State final minimal set and reason for each condition pair — this is what examiners expect.

Common mistakes

  • Miscounting nodes or edges when computing cyclomatic complexity — always identify basic blocks, not source lines.
  • Treating cyclomatic complexity as the total number of paths; it is the number of independent paths (basis) — examiners often penalize this confusion.
  • Trying to achieve full path coverage for programs with loops without bounding iterations (infinite paths) — state loop bounds if you claim a percentage.
  • Applying MC/DC incorrectly by not keeping other conditions fixed when showing a condition's independent effect.

For your exam

Types of questions you'll see: definitions (short), short notes (compare coverage criteria), derivations (derive V(G) = E − N + 2P or V = D+1 under assumptions), numericals (draw CFG and compute coverage %, compute V(G)), trace and design problems (given code, produce tests to achieve 100% statement/branch/MC/DC), diagram tasks (draw CFG and label edges), and small proofs (show why 100% branch ⇒ 100% statement under common assumptions).

What a full-marks answer includes:

  • Clear definitions with bold term naming (statement, branch, path, decision, condition, MC/DC, CFG).
  • A drawn CFG (boxes for basic blocks, labelled edges; include entry/exit), or a clear textual description if drawing is not possible.
  • Correct counts and step-by-step computation (lists of executed statements/branches for each test case; explicit fractions and final percentages).
  • For cyclomatic complexity, state N, E, P and compute V(G) showing substitution.
  • For MC/DC, show explicit test pairs for each atomic condition and explain why other conditions are held fixed and why decision outcome toggles.

Quick check

Q1: Define branch coverage in one sentence. Q2: State the cyclomatic complexity formula using edges (E), nodes (N) and components (P). Q3: For the decision D = A || B, give a minimal test set satisfying MC/DC and briefly explain.

Answers: Q1: Branch coverage = fraction of decision outgoing edges in the CFG taken by the test suite.; Q2: V(G) = E - N + 2P.; Q3: Minimal MC/DC tests: (A=false,B=false), (A=true,B=false), (A=false,B=true). For A: compare (T,F) vs (F,F) — only A differs and the decision toggles. For B: compare (F,T) vs (F,F) — only B differs and the decision toggles.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?