Path testing

Path testing is a white box test design technique in which test cases are chosen to execute particular paths through a program's control flow, from entry to exit. Covering every path is impractical once the code contains a loop, so testers work from a selected set, most often the basis paths counted by cyclomatic complexity.

The longer version

A path is one complete route through a piece of code, from its first statement to a return, taking a particular outcome at every decision on the way. The routes are easiest to see on a control flow graph, in which blocks of straight-line code are nodes and the possible jumps between them are edges. Consider a function that prices a parcel. It adds a surcharge if the parcel is heavy, adds another if the address is on an island, and applies a discount if the sender holds an account. Three decisions in a row, each with two outcomes, give 2 × 2 × 2 = 8 paths. Two tests are enough to take every decision both ways, one with all three conditions true and one with all three false, and those two tests never send a light parcel to an island for an account holder. Path testing is concerned with that gap: a defect that shows only when particular outcomes occur together, such as a discount worked out before the surcharge it should have included.

Testing every path sounds like the thorough answer and is out of reach for ordinary code. Decisions in sequence multiply: ten of them give 2 multiplied by itself ten times, which is 1,024 paths, and twenty give 1,048,576. Loops are worse. A loop that may run zero, one, two or any number of times contributes a different path for each count, and NIST Special Publication 500-235 notes that most programs with a loop have a potentially infinite number of paths, which are not subject to exhaustive testing even in theory. Some routes that appear on the graph cannot be executed at all, since two conditions on one variable may contradict each other, and working out which are feasible is a job in itself. Path testing in practice therefore means choosing a set of paths small enough to run, by a rule that can be stated. For a loop the usual rule is to take it zero times, once and several times, with a further test at its maximum where one exists.

The best-known rule is basis path testing. NIST Special Publication 500-235, written by Arthur Watson and Thomas McCabe and published in 1996, presents it under the name structured testing and bases it on the cyclomatic complexity measure of McCabe, citing his paper A Complexity Measure in IEEE Transactions on Software Engineering, December 1976. The report defines cyclomatic complexity on the control flow graph as the number of edges minus the number of nodes plus two. For code whose decisions are all two-way it gives a shortcut, the number of decisions plus one, so the parcel function scores 3 + 1 = 4. That number is the size of a basis set: a group of independent paths from which, by the report's account, all possible paths can be generated by linear combination. Four tests chosen this way exercise each decision outcome independently, which is more than the two tests that took every decision both ways and fewer than the eight of all paths. The report also records that McCabe originally proposed 10 as a limit on a module's complexity. A basis set leaves some combinations unrun, and like every white box technique it cannot find a requirement that was never coded.

Applied by hand, basis path testing takes five steps: draw the control flow graph, work out the cyclomatic complexity, choose that many independent paths, find input data that drives each one, and run the tests against their expected results. NIST Special Publication 500-235 gives a rule for the third step, which it calls the baseline method: take one baseline path, then build each further path by following an earlier one up to a decision and taking that decision's other outcome. For the parcel function the baseline might be a light parcel to the mainland from a sender with no account, and the other three paths flip one decision each: heavy, island, account holder. Each differs from what came before in one decision, which is what makes the four independent. Textbooks often shrink the graph first by merging each run of nodes between two decisions into one, giving decision-to-decision paths, or DD-paths. The same idea also works above the code. TMAP applies path coverage to a business process flow, where the technique is called a process cycle test, and grades its thoroughness by test depth level. Level 1, in TMAP's words, provides the guarantee that each path has been travelled at least once, and level 2 combines every path into a decision point with every path out of it. TMAP also states the limit the code version shares: the method does not consider the conditions that are in the decisions.

Common questions

What is path testing in software testing?

Path testing is a white box technique that designs test cases to execute chosen paths through a program, where a path is one complete route from entry to exit with a particular outcome at each decision. Since the number of possible paths is usually too large to run, the technique rests on a rule for selecting which ones to test.

What is basis path testing?

Basis path testing is path testing with a specific selection rule: test a basis set of paths, a group of independent paths whose number equals the code's cyclomatic complexity. NIST Special Publication 500-235, by Arthur Watson and Thomas McCabe, presents it as structured testing and states that it requires each decision outcome to be tested independently, a stricter demand than branch coverage makes.

What is cyclomatic complexity?

Cyclomatic complexity is a measure of the amount of decision logic in a software module, credited to Thomas McCabe and his 1976 paper A Complexity Measure. NIST Special Publication 500-235 defines it on the control flow graph as edges minus nodes plus two. Where every decision is two-way, the same figure is the number of decisions plus one. For a graph made of several separate parts, the general form adds twice the number of parts in place of the two.

What is path coverage in white box testing?

Path coverage is the share of a program's possible paths, from entry to exit, that the tests have executed. Full path coverage means every distinct route through the code has run, which is achievable for short code without loops and out of reach for most real functions. In practice teams measure coverage of a chosen set instead, such as the basis paths or, for a business process, every path through each decision point.

What are the steps of basis path testing?

Draw the control flow graph of the code. Calculate its cyclomatic complexity, which gives the number of paths needed. Select that many independent paths, for instance by the baseline method: one baseline route, then one path for each decision flipped in turn. Find input data that drives the code down each path, write the expected result for each, and run the tests.

Why is testing every path impractical?

Paths multiply with every decision and become unbounded with every loop. Five two-way decisions in sequence give 2 × 2 × 2 × 2 × 2 = 32 paths, and a loop adds a new path for each number of times it can run. Some paths on the control flow graph can never be executed, and finding inputs for the ones that can is laborious. Testers select paths by a stated rule instead.

What is the difference between path coverage and branch coverage?

Branch coverage is satisfied when every decision has gone each way at least once, in any combination. Path coverage looks at whole routes, so it distinguishes the test in which the first decision was true and the second false from the test in which both were true. Three two-way decisions in sequence need two tests for full branch coverage and eight to cover every path.

Bug reports that arrive ready to triage.

Collect replay, console, network and browser context with the Free plan.

<script src="…/widget.js" data-key="pb_pub_…">