White Box Techniques
Statement Coverage
Mỗi line code executed at least once.
def discount(price, vip):
if vip:
return price * 0.8
return price * 0.95
Test 1: vip=True → covers if branch.
Test 2: vip=False → covers else.
2 tests = 100% statement coverage.
Branch Coverage
Mỗi branch (true/false of each condition) covered.
Same example above → same 2 tests suffice.
Condition Coverage
Each boolean sub-condition gets both true/false.
if vip and country == "VN":
return discount
- (vip=T, country=VN) → both T.
- (vip=F, country=US) → both F.
2 tests cover condition coverage.
Path Coverage
Every possible execution path.
- Exponential in nested conditions.
- Rarely 100% feasible.
MC/DC (Modified Condition/Decision Coverage)
Aviation/safety standard. Each condition independently affects outcome.
Loop Coverage
- 0 iteration.
- 1 iteration.
- > 1 iteration.
- Boundary (N-1, N, N+1) if N has limit.
Tools
- Jest --coverage (JS).
- Coverage.py (Python).
- JaCoCo (Java).
- Slather (iOS).
Anti-pattern
- 100% line coverage = no quality assurance. Test execute code but no assert.
- Skip difficult code (catch block) → low value coverage.
- Mock too much → don't test real behavior.
Mutation testing
Insert random bug → does test catch?
- Stryker (JS), PIT (Java).
- Real measure of test quality.
- Run weekly, not every commit (slow).
VN context
Banking critical service: MC/DC mandatory. SBV inspector ask for evidence.