The idea: follow the values, not just the lines
Statement and branch coverage ask which lines and edges ran. Data-flow testing asks a different question: did each value that the code computes reach the places that read it? A wrong value only causes a wrong result if it travels from where it was made to where it is used. So the tests should make it travel.
Three words do all the work:
- A definition (def) gives a variable a value:
total = qty * 10definestotal. Parameters are defined when the function is called (node 1). - A c-use (computation use) reads a variable to compute something:
qty * 10c-usesqty,return totalc-usestotal. - A p-use (predicate use) reads a variable in a condition:
if (total > 100)p-usestotal. The use decides which edge is taken, so it is counted on each out-edge: once for T, once for F.
The function on the canvas, bill(qty, member), has a spec: items cost 10 each,
members get 5 off, and the bill is at most 100. Node 4 has a sign bug: it writes
total + 5 where it should write total - 5.
Demo 1: defs, uses and the 11 def-use pairs walks through the notes next to the code.
Def-use pairs and def-clear paths
A def-use pair (x, d, u) is a def of x at node d and a use of x at u, such that some path from d to u has no other def of x on it. That path is def-clear for x. If a path goes through another def of x, the old value is overwritten on the way: the new def kills it (this "kill" is not the one in mutation testing).
To find the pairs, start at each def and walk forward along the edges. Record every use you meet.
Stop on a branch as soon as you meet another def of the same variable. That is a
reaching definitions search, the same one a compiler runs.
bill has 5 defs and 11 pairs:
| # | Pair (var, def, use) | Kind | Def-clear path | Killed on |
|---|---|---|---|---|
| 0 | (qty, 1, 2) | c-use | 1-2 | none |
| 1, 2 | (member, 1, 3→4) and (member, 1, 3→5) | p-use | 1-2-3-4, 1-2-3-5 | none |
| 3 | (total, 2, 4) | c-use | 2-3-4 | none |
| 4, 5 | (total, 2, 5→6) and (total, 2, 5→7) | p-use | 2-3-5-6, 2-3-5-7 | routes through node 4 |
| 6 | (total, 2, 7) | c-use | 2-3-5-7 | routes through 4 or 6 |
| 7, 8 | (total, 4, 5→6) and (total, 4, 5→7) | p-use | 4-5-6, 4-5-7 | none |
| 9 | (total, 4, 7) | c-use | 4-5-7 | route through 6 |
| 10 | (total, 6, 7) | c-use | 6-7 | none |
That is 5 c-use pairs and 6 p-use pairs. Pair 9 matters most here: the value made by the buggy
node 4 reaches the return only along 4-5-7, when the total is not capped. On the other route,
node 6 sets total = 100 and the bad value is gone.
Demo 2: a redefinition kills: total from node 4 shows both routes.
Demo 7: def-clear only when both ifs are false shows pair 6, which two kills leave with
a single path.
The data-flow criteria
Rapps and Weyuker (1985) defined a family of criteria on these pairs. Each one names the pairs a test suite must cover. A test covers a pair when it walks the def, then a def-clear path, then the use. The canvas tracks the reaching def of every variable while a test runs, so each use shows which pair it covers.
| Criterion | Cover | Pairs to cover in bill | Tests needed here |
|---|---|---|---|
| all-defs | for every def, at least one pair | 5 defs | 1 (T1) |
| all-c-uses | every c-use pair | 5 | 3 (T1, T2, T4) |
| all-p-uses | every p-use pair | 6 | 4 |
| all-uses | every pair | 11 | 4 |
| branch coverage, for comparison | every T and F edge | 4 edges | 2 |
all-uses includes all-c-uses and all-p-uses. all-p-uses includes branch coverage, because every out-edge of a decision carries at least one p-use pair. Here all-p-uses needs more tests than branch coverage: it asks for the cap test with the value from node 2 and with the value from node 4. The criteria stop short of path coverage, which a loop makes endless; all-uses stays finite.
The bug branch coverage misses
The four tests, with the spec's answer:
| Test | Spec | Code returns | Path | Pairs covered |
|---|---|---|---|---|
T1 bill(12, true) | 100 | 125, capped to 100 ✓ | TT | 0 1 3 7 10 |
T2 bill(3, true) | 25 | 35 ✗ | TF | 0 1 3 8 9 |
T3 bill(12, false) | 100 | 100 ✓ | FT | 0 2 4 10 |
T4 bill(3, false) | 30 | 30 ✓ | FF | 0 2 5 6 |
T1 alone already meets all-defs: each of the 5 defs reaches some use. It passes, because the cap at node 6 hides the wrong 125 (Demo 3: all-defs with one test, bug missed). Add T4 and every branch is taken, 4 of 4, and both tests are still green. Yet pairs 4, 8 and 9 are pink: the value from node 4 has never reached the return (Demo 4: 100% branches, three pairs still missed).
all-c-uses asks for pair 9, and the only path for it is 4-5-7: a member with a small order. T2 is that test, and it fails with 35 instead of 25 (Demo 5: all-c-uses, pair (total, 4, 7) finds the bug). T3 then covers pair 4 and the suite meets all-uses (Demo 6: all-uses, four tests, 11 of 11 pairs).
Covering a pair is still not a proof. T2 finds the bug only because it compares 35 with the spec's 25. A test that covers pair 9 but checks nothing would stay green. Mutation testing measures whether the checks are strong enough.
Data-flow testing in practice
Compilers already compute reaching definitions and live variables for optimisation and register allocation, so a tool can list the pairs. Few everyday coverage tools report them, though; JaCoCo, gcov and coverage.py stop at lines and branches. Data-flow criteria are used most in course work, in research tools, and as a way to choose extra tests by hand: look for a def whose value never reached an output in any test. Static analysers flag the related data-flow anomalies: a use with no def before it, or a def that is overwritten before any use.
See also Control-Flow Coverage for statement, branch and path coverage on the same kind of graph, MC/DC Coverage for conditions made of several parts, Mutation Testing for checking that the tests' asserts can tell a wrong result, Boundary Value Testing for picking input values from the spec, and Register Allocation for a compiler's liveness analysis.
See also Pairwise Testing, a black-box technique that picks test inputs so that every pair of parameter values is tried at least once.
What the page leaves out
Loops (pairs that need a path around the loop), arrays and pointers (a def of a[i]
may or may not kill a[j]), calls between functions (interprocedural pairs), the
weaker mixed criteria all-c-uses/some-p-uses and all-p-uses/some-c-uses, all-du-paths (every
simple def-clear path, not just one), infeasible pairs that no input can cover, and how tools
instrument code to track pairs are left out.