Basis path testing is a white box testing technique that enables the test designer to derive a logical complexity measure and use it to define a basis set of execution paths.
The Cyclomatic Complexity metric is a software metric that provides a quantitative measure of the logical complexity of a program. The value computed for cyclomatic complexity defines the number of independent paths in the basis set of a program and provides the upper bound for the number of tests that must be conducted to guarantee that all statements have been executed at least once.
An independent path is any path through the program that introduces at least one new statement or condition. Regarding a flow graph, an independent path must move along at least one edge that has not been traversed before.
Cyclomatic complexity is computed in one of the following ways:
The number of regions of the flow graph corresponds to the cyclomatic complexity.
Cyclomatic Complexity, V( G ), for a flow graph G is defined as:
V( G ) = E – N + 2
- E is the number of flow graph edges
- N is the number of flow graph nodes.
Cyclomatic complexity, V( G ), for a flow graph G is defined as:
V( G ) = P + 1
- P is the number of predicate nodes contained in the flow graph G.
The value for V( G ) provides an upper bound on the number of tests that must be designed and executed to guarantee coverage of all program statements.
Basis Path Testing
Test cases derived from exercising the basis paths are guaranteed to execute every statement in the program at least once during testing.
The basis path testing method can be applied in the following way:
- using the source code, draw the corresponding flow graph
- determine the cyclomatic complexity of the resultant flow graph
- determine the basis set of independent paths
- prepare test cases that will force execution of each path in the basis set