White Box Testing
What is White Box Testing?
White box testing (also called structural or glass-box testing) examines the internal code structure, logic, and paths of an application, rather than just its outputs.
Key Characteristics
- Requires programming knowledge to design test cases based on the code itself.
- Focuses on statement coverage, branch coverage, and path coverage.
- Typically performed by developers during unit and integration testing.
- Helps optimise code, remove dead code, and uncover hidden logical errors.
Common White Box Techniques
- Statement coverage testing.
- Branch/decision coverage testing.
- Path coverage testing.
- Loop testing.
Designing Tests from a Real Function
White box test design starts by reading the code and listing every decision. Consider this small Java method:
public String grade(int marks) {
if (marks < 0 || marks > 100) {
return "Invalid";
}
if (marks >= 40) {
return "Pass";
}
return "Fail";
}
Now walk through it step by step:
- Draw the flow: there are two
ifdecisions and three possible exits. - Find the cyclomatic complexity: with two decisions, complexity is 2 + 1 = 3, so at least three independent paths exist.
- Pick inputs for each path: −5 returns "Invalid", 75 returns "Pass", and 20 returns "Fail".
- Check condition coverage: the first decision has two conditions joined by OR. The value −5 only makes the left side true, so add 150 to make the right side true as well.
- Add boundaries: 0, 39, 40 and 100 catch off-by-one mistakes such as writing
>instead of>=.
Coverage Levels Compared
| Coverage type | What must be exercised | Strength |
|---|---|---|
| Statement | Every line runs at least once | Weakest; misses untaken branches |
| Branch / decision | Every true and false outcome | Catches missing else logic |
| Condition | Every sub-condition true and false | Finds faulty compound conditions |
| Path | Every route through the code | Strongest, but explodes with loops |
Full branch coverage always gives full statement coverage, but the reverse is not true. That is why teams usually set their target on branches rather than lines.
Measuring Coverage with Tools
You rarely calculate coverage by hand on real projects. Tools instrument the code while tests run and produce a report showing covered and missed lines and branches.
- Java: JaCoCo, usually added as a Maven or Gradle plugin.
- Python: coverage.py, often run through pytest-cov.
- JavaScript: Istanbul, built into Jest via the
--coverageflag.
mvn clean test jacoco:report
# open target/site/jacoco/index.html to see missed branches
Red or yellow markers in the report point straight to branches no test has taken, which become your next test cases.
Where White Box Testing Falls Short
Structural tests only check the code that exists. If a requirement was never implemented, no coverage tool will notice the gap. That is why white box work is paired with black box testing based on specifications.
- High coverage does not mean correct behaviour; a test can execute a line without asserting anything useful.
- Chasing a coverage percentage can produce shallow tests written only to turn lines green.
- Tests tied tightly to internal structure may break during harmless refactoring.
Practical checklist: map decisions, compute complexity, cover every branch, split compound conditions, add boundary values, run a coverage tool, and make sure every test has a meaningful assertion.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.