Q32 of 38 · Test design
How do you approach branch coverage for a complex conditional logic block?
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
elseof anifwithout an explicitelseblock (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
// Related questions