Basis Path Testing in Software Testing

Last Updated : 21 Jul, 2025

Path Testing is a white-box testing technique that focuses on executing all possible execution paths within a program or module's control structure to ensure code coverage. Using this structure, a control flow graph is prepared, where nodes represent code statements or blocks, and edges show how control moves between them. All unique paths, including decision points like if-else statements and loops, are identified and tested with specific test cases.

Path Testing Process

Path Testing process is the technique where the test cases are designed to cover all possible paths within the program's control flow. The goal is to ensure that every independent execution path is tested. Path testing typically follows four steps:

  1. Construct the Control Flow Graph.
  2. Compute the Cyclomatic Complexity of the Graph.
  3. Identify the Independent Paths.
  4. Design Test cases from Independent Paths.

Let's understand each step one by one.

1. Control Flow Graph

A control flow graph (or simply, flow graph) is a directed graph that represents the control structure of a program or module. A control flow graph (V, E) has V number of nodes/vertices and E number of edges in it. Control graph will include the components which are:

  • Junction Node - a node with more than one arrow entering it.
  • Decision Node - a node with more than one arrow leaving it.
  • Region - area bounded by edges and nodes (area outside the graph is also counted as a region).
Types-of-Nodes
Types of Node- Region-1

Here is the Region-2

Region-2
Region-2

Notations of Flow Graph:

Control Flow Graph (CFG), notations represent the control structures of a program. These notations are used to construct the CFG, which visualizes the flow of execution with nodes (statements or blocks) and edges.

1. Sequential Statements:

Represents a linear sequence of statements executed one after another without branching. In the CFG, each statement is a node, and edges connect them in execution order (e.g., Statement1 → Statement2).

Sequential-Statements
Sequential Statements

2. If - Then - Else

Represents a conditional statement with two possible paths: one for the "then" branch (if the condition is true) and one for the "else" branch (if false). The decision node (condition) has two outgoing edges to separate nodes for each branch, which may converge at a junction node.

If-Then-Else
If - Then - Else

3. Do - While

Represents a loop where a block of statements is executed at least once, followed by a condition check. The CFG has a loop with a decision node at the end (condition) with one edge looping back to the start of the block and another exiting the loop.

Do-While
Do - While

4. While - Do

Represents a loop where a condition is checked before executing a block of statements. The CFG starts with a decision node (condition) with one edge to the statement block (if true) and another to exit the loop (if false), with a loop-back edge from the block to the condition.

While-Do
While - Do

5. Switch - Case

Represents a multi-way branching structure where a variable is evaluated, and control jumps to one of several case blocks. The CFG has a decision node (switch expression) with multiple outgoing edges, each leading to a case block, and possibly a default branch, converging at a junction node.

Switch-Case
Switch - Case

2. Compute Cyclomatic Complexity

Cyclomatic Complexity - Cyclomatic Complexity is a metric used to measure the logical complexity of a program by analyzing its Control Flow Graph (CFG). It quantifies the number of linearly independent paths through a program, indicating the minimum number of test cases needed for full path coverage.

The cyclomatic complexity V(G) of a control flow graph G is calculated as:

V(G) = e - n + 2*P

Where, e is number of edges, n is number of vertices, P is number of connected components. For example, consider first graph given above,

where, e = 4, n = 4 and p = 1 

So,
Cyclomatic complexity V(G) 
= 4 - 4 + 2 * 1 
= 2 

Formula based on Decision Nodes:

V(G) = d + P 

where, d is number of decision nodes, P is number of connected nodes. For example, consider first graph given above,

where, d = 1 and p = 1

So, 
Cyclomatic Complexity V(G) 
= 1 + 1 
= 2 

Formula based on Regions :

V(G) = number of regions in the graph

For example, consider first graph given above,

Cyclomatic complexity V(G) 
= 1 (for Region 1) + 1 (for Region 2)
= 2 

Hence, using all the three above formulae, the cyclomatic complexity obtained remains same. All these three formulae can be used to compute and verify the cyclomatic complexity of the flow graph.

Note -

  1. For one function [e.g. Main( ) or Factorial( ) ], only one flow graph is constructed. If in a program, there are multiple functions, then a separate flow graph is constructed for each one of them. Also, in the cyclomatic complexity formula, the value of 'p' is set depending of the number of graphs present in total.
  2. If a decision node has exactly two arrows leaving it, then it is counted as one decision node. However, if there are more than 2 arrows leaving a decision node, it is computed using this formula :
d = k - 1

Here, k is number of arrows leaving the decision node.

3. Identify Independent Paths

An independent path in the control flow graph is the one which introduces at least one new edge that has not been traversed before the path is defined. The cyclomatic complexity gives the number of independent paths present in a flow graph. This is because the cyclomatic complexity is used as an upper-bound for the number of tests that should be executed in order to make sure that all the statements in the program have been executed at least once.

Consider first graph given above here the independent paths would be 2 because number of independent paths is equal to the cyclomatic complexity. So, the independent paths in above first given graph :

Path 1: 

A -> B 

Path 2: 

C -> D 

Note - Independent paths are not unique. In other words, if for a graph the cyclomatic complexity comes out be N, then there is a possibility of obtaining two different sets of paths which are independent in nature.

4. Design Test Cases

Finally, after obtaining the independent paths, test cases can be designed where each test case represents one or more independent paths. It map each independent path to one or more test cases, using inputs that force the program to follow specific paths.

Advantages of Basis Path Testing

Here are the advantages of basis path testing:

  1. Basis path testing provides the best code coverage as it aims to achieve maximum logic coverage instead of maximum path coverage. This results in an overall thorough testing of the code.
  2. When a software is modified, it is still necessary to test the changes made in the software which as a result, requires path testing.
  3. When a developer writes the code, he or she tests the structure of the program or module themselves first. This is why basis path testing requires enough knowledge about the structure of the code.
  4. When one module calls other modules, there are high chances of Interface errors. In order to avoid the case of such errors, path testing is performed to test all the paths on the interfaces of the modules.
  5. Since the basis path testing technique takes into account the complexity of the software (i.e., program or module) while computing the cyclomatic complexity, therefore it is intuitive to note that testing effort in case of basis path testing is directly proportional to the complexity of the software or program.

Disadvantages of Basis Path Testing

Here are the few dis-advantages of basis path testing:

  • High cyclomatic complexity requires numerous test cases, increasing effort.
  • Focuses on logical paths, potentially missing data-related or integration issues.
  • It demands detailed understanding of the codebase.
  • Must be combined with other testing types.

By implementing Control Flow Graph and cyclomatic complexity, it systematically validates logical correctness, making it ideal for unit and integration testing. While it requires code knowledge and can be time-consuming for complex programs, modern tools and best practices streamline the process.

Comment

Explore