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.

The function bill drawn as seven control-flow graph nodes. Each node is labelled with what it defines and uses: node 1 defines qty and member, node 2 uses qty and defines total, node 3 has a p-use of member on its T and F edges, node 4 uses and defines total, node 5 has a p-use of total, node 6 defines total, node 7 uses total
Every node defines some variables and uses others. A use in a condition (p-use) belongs to the decision's out-edges.

Three words do all the work:

  • A definition (def) gives a variable a value: total = qty * 10 defines total. Parameters are defined when the function is called (node 1).
  • A c-use (computation use) reads a variable to compute something: qty * 10 c-uses qty, return total c-uses total.
  • A p-use (predicate use) reads a variable in a condition: if (total > 100) p-uses total. 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)KindDef-clear pathKilled on
0(qty, 1, 2)c-use1-2none
1, 2(member, 1, 3→4) and (member, 1, 3→5)p-use1-2-3-4, 1-2-3-5none
3(total, 2, 4)c-use2-3-4none
4, 5(total, 2, 5→6) and (total, 2, 5→7)p-use2-3-5-6, 2-3-5-7routes through node 4
6(total, 2, 7)c-use2-3-5-7routes through 4 or 6
7, 8(total, 4, 5→6) and (total, 4, 5→7)p-use4-5-6, 4-5-7none
9(total, 4, 7)c-use4-5-7route through 6
10(total, 6, 7)c-use6-7none

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.

Two copies of the lower part of the graph, from node 4 to node 7. Left: the route 4-5-6-7 passes node 6, which redefines total, so the value from node 4 is killed and never reaches the return. Right: the route 4-5-7 has no other def of total, so it is def-clear and covers the pair (total, 4, 7)
A redefinition on the way kills the old value: only the route that skips node 6 carries total from node 4 to the return.

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.

CriterionCoverPairs to cover in billTests needed here
all-defsfor every def, at least one pair5 defs1 (T1)
all-c-usesevery c-use pair53 (T1, T2, T4)
all-p-usesevery p-use pair64
all-usesevery pair114
branch coverage, for comparisonevery T and F edge4 edges2

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:

TestSpecCode returnsPathPairs covered
T1 bill(12, true)100125, capped to 100 ✓TT0 1 3 7 10
T2 bill(3, true)2535 ✗TF0 1 3 8 9
T3 bill(12, false)100100 ✓FT0 2 4 10
T4 bill(3, false)3030 ✓FF0 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).

Three test suites side by side. T1 alone: all-defs met, branches 2 of 4, pairs 5 of 11, bug missed. T1 and T4: branches 4 of 4, pairs 8 of 11, pair 9 not covered, bug missed. T1, T4 and T2: all c-uses met, pairs 10 of 11, T2 fails and the bug is found
100% branch coverage leaves pair 9 uncovered; asking for it brings in the one test that fails.

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.

On the mind map

Open the Software Design mind mapSoftware DesignPrinciples: keep each change localPrinciplesDesign patterns: named fixes for common designsDesign patternsArchitecture: the shape of a whole appArchitectureEnterprise app patterns: logic, SQL, locks, money (PoEAA)Enterprise app patternsGit storage: content named by its hashGit storageGit branches & merging: pointers to commits, then mergeGit branches & mergingTesting: check code in isolationTestingSOLIDSOLIDfive rules so one change hits one classInheritance vs compositionInheritance vs compositionis-a locks the tree; has-a mixes partsVirtual calls, vtableVirtual calls, vtableobject → vptr → vtable slot → functionC++ object layoutC++ object layoutB* to a C adds 16; virtual base is sharedOverloading vs overridingOverloading vs overridingjavac: static types; JVM: receiver's classCoupling and cohesionCoupling and cohesionOne change should touch one moduleControl-flow coverageControl-flow coverage100% statements can still miss a bugDef-use pairs (data flow)A wrong def shows only if it reaches a usePairwise testingPairwise testing9 tests cover all 54 pairs, not 81Boundary Value TestingBoundary Value TestingBugs sit at class edges: test min−1 … max+1Condition Coverage and MC/DCCondition Coverage and MC/DCEach condition flips D alone: N+1 testsMutation testingMutation testingCoverage runs code; mutants test the assertsOpen the Software Design mind mapSoftware Designkeep each change localPrinciplesSOLIDSOLIDfive rules so one change hits one classInheritance vs compositionInheritance vs compositionis-a locks the tree; has-a mixes partsVirtual calls, vtableVirtual calls, vtableobject → vptr → vtable slot → functionC++ object layoutC++ object layoutB* to a C adds 16; virtual base is sharedOverloading vs overridingOverloading vs overridingjavac: static types; JVM: receiver's classCoupling and cohesionCoupling and cohesionOne change should touch one moduleControl-flow coverageControl-flow coverage100% statements can still miss a bugDef-use pairs (data flow)A wrong def shows only if it reaches a usePairwise testingPairwise testing9 tests cover all 54 pairs, not 81Boundary Value TestingBoundary Value TestingBugs sit at class edges: test min−1 … max+1Condition Coverage and MC/DCCondition Coverage and MC/DCEach condition flips D alone: N+1 testsMutation testingMutation testingCoverage runs code; mutants test the asserts
This page on the Software Design mind map · 43 pages in 7 branches · open it to see the whole map and test yourself in recall mode.