Structure Based or White Box Testing Techniques
What are Structure-Based Techniques?
Structure-based (white box) techniques derive test cases from the internal structure of the code, such as its statements, branches, and paths, rather than from external specifications.
Common Structure-Based Techniques
- Statement Coverage: Ensuring every line of code is executed at least once.
- Branch Coverage: Ensuring every decision point (true/false branch) is executed.
- Path Coverage: Ensuring every possible path through the code is executed.
- Cyclomatic Complexity & Basis Path Testing: Using code complexity metrics to design a minimal, thorough set of test paths.
Why Use White Box Techniques?
These techniques help uncover dead code, unreachable paths, and logical errors that black box testing — which only looks at inputs and outputs — might not detect.
Worked Example: Measuring Coverage on One Function
Take this small Java method that calculates a discount percentage:
int discount(int amount, boolean member) {
int d = 0;
if (amount > 1000) { // decision 1
d = 10;
}
if (member) { // decision 2
d = d + 5;
}
return d;
}
Statement coverage
A single test, discount(1500, true), enters both if blocks and executes every statement, giving full statement coverage. Yet it never checks what happens when a condition is false.
Branch coverage
Each decision needs a true outcome and a false outcome. Two tests are enough: (1500, true) makes both decisions true, and (500, false) makes both false. Every branch has now been taken at least once.
Path coverage
Two independent decisions create four routes: true-true, true-false, false-true and false-false. Adding (1500, false) and (500, true) completes them. With loops, the number of paths can explode, which is why full path coverage is rarely practical on real code.
Cyclomatic Complexity and Basis Paths
Cyclomatic complexity, written V(G), counts the independent paths through code. Two quick formulas give the same answer:
- V(G) = E - N + 2, using the edges and nodes of the control flow graph.
- V(G) = number of decision points + 1, which is easier by hand.
The discount method has two decisions, so V(G) = 3. Basis path testing needs three tests in which each new test introduces at least one new branch, for example (500, false), (1500, false) and (500, true). Functions with a high complexity value are harder to test and are good candidates for refactoring.
Beyond Branches: Condition-Level Techniques
When a decision contains several conditions, such as if (age >= 18 && hasId), branch coverage may miss faults in one condition. Stronger criteria exist:
| Criterion | Requirement |
|---|---|
| Condition coverage | Each individual condition is evaluated as both true and false |
| Condition/decision coverage | Both conditions and whole decisions take both outcomes |
| MC/DC | Each condition is shown to independently change the decision's outcome; common in safety-critical software |
Tools and Practical Advice
Coverage is measured by tools rather than by counting lines manually. JaCoCo is common for Java, coverage.py for Python, and Istanbul (through nyc or built into Jest) for JavaScript. They produce reports highlighting lines and branches that tests never reached.
- Use reports to find untested logic, not as a score to chase. Tests that run code without asserting anything raise coverage but catch nothing.
- Full statement coverage does not mean the code is correct; missing requirements never show up as uncovered lines.
- Pair white box tests with black box tests, because each finds defects the other misses.
- Target error-handling branches deliberately; they are the parts most often left untested.
- Review coverage of changed lines in each pull request, which keeps the effort small and focused.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.