Q32 of 38 · Test design

How do you approach branch coverage for a complex conditional logic block?

Test designMidtest-designbranch-coveragecyclomatic-complexitystructural-testingdecision-table

Short answer

Short answer: Enumerate every decision point and its outcomes, then design one test case per branch. For an if/else chain with N conditions, you need at least N+1 test cases to cover every branch — more if conditions interact.

Detail

Start with a decision table or a flow diagram of the conditional logic. Each binary decision (true/false, yes/no) doubles the number of branches. Three chained if-statements with no else produce 4 possible paths through the code.

For the design, work backwards from the branches you want to cover: "to reach the catch block, the API call must throw — what input causes that?" Then construct the test data that produces that condition.

Common branches that are systematically missed:

  • The else of an if without an explicit else block (the implicit do-nothing case)
  • Short-circuit evaluation: in A && B, if A is always true in tests, B's false case is never exercised
  • Default cases in switch statements
  • Exception-handling paths

For functions with more than 5–6 branches, consider whether the function should be decomposed — high cyclomatic complexity is both a test design problem and a code quality signal.

// WHAT INTERVIEWERS LOOK FOR

Systematic enumeration of branches from the code structure. Backwards design from the condition to the input. Identifying systematically-missed branches (else, short-circuit, exceptions).