White Box Testing Techniques
White Box Testing Techniques
Understanding structural testing methods that examine the internal logic and code structure of software
Statement Coverage
Ensures every executable statement in the code is executed at least once.
Basic coverage criterion | Easy to achieve
Branch Coverage
Ensures each decision point (branch) in the code is taken in all possible directions.
Stronger than statement coverage | Tests all outcomes
Condition Coverage
Ensures each boolean sub-expression evaluates to both true and false.
Tests individual conditions | More thorough than branch coverage
Multiple Condition Coverage
Tests all possible combinations of conditions in a decision.
Most comprehensive | Can generate many test cases
Basis Path Testing
Tests all linearly independent paths through a program's control flow.
Uses cyclomatic complexity | Systematic approach
Cyclomatic Complexity
Quantitative measure of the number of linearly independent paths through code.
Metric for complexity | Guides testing effort
Loop Testing
Focuses on validating the correctness of loop constructs.
Tests loop boundaries | Specialized technique
Statement Coverage
Basic CoverageWhat is Statement Coverage?
Statement coverage is a white box testing technique that aims to execute every executable statement in the source code at least once. It's the most basic form of code coverage.
How it Works
The goal is to create test cases that ensure no statement in the code is left unexecuted. This technique helps identify:
- Dead code (code that cannot be reached)
- Missing statements
- Basic functionality issues
Limitations
While statement coverage is easy to achieve, it doesn't guarantee thorough testing because:
- It doesn't test all decision outcomes
- It may miss errors in conditional logic
- It doesn't verify the correctness of the statements
Code Example
let grade;
if (score >= 90) {
grade = 'A'; // Statement 1
} else if (score >= 80) {
grade = 'B'; // Statement 2
} else {
grade = 'C'; // Statement 3
}
return grade; // Statement 4
}
Coverage Visualization
To achieve 100% statement coverage, we need test cases that execute all statements:
✅ 100% Statement Coverage Achieved
Branch Coverage
Decision CoverageWhat is Branch Coverage?
Branch coverage (also called decision coverage) ensures that each decision point in the code (like if statements) takes both true and false paths at least once.
How it Works
This technique requires test cases that cause each branch direction to be executed. It's stronger than statement coverage because it verifies the decision logic.
Advantages Over Statement Coverage
- Tests both outcomes of decisions
- Helps find errors in conditional logic
- Provides better code exercise than statement coverage
Example
For an if-else statement, branch coverage requires:
- One test where the condition is true
- One test where the condition is false
Code Example
if (age >= 18) { // Branch 1
if (hasLicense) { // Branch 2
return true;
} else {
return false;
}
} else {
return false;
}
}
Branch Coverage Requirements
Test cases needed for branch coverage:
- age=20, hasLicense=true (True/True path)
- age=20, hasLicense=false (True/False path)
- age=16, hasLicense=any (False path)
Condition Coverage
Condition TestingWhat is Condition Coverage?
Condition coverage ensures that each boolean sub-expression within a decision evaluates to both true and false.
How it Differs from Branch Coverage
While branch coverage tests the overall decision outcome, condition coverage tests each individual condition within compound decisions.
Example
For a condition like (A && B), condition coverage requires:
- A true, B true
- A true, B false
- A false, B true
- A false, B false
Code Example
shutdownSystem();
}
Comments
Post a Comment