Related Tags

software testing

# What is basis path testing?

Kainat Asif

Basis path testing is a type of white-box testing that tests all possible independent paths in the control flow graph of a program.

Note:

• A path is the route of nodes in the control flow graph that a program takes from one point to another.
• An independent path is a path that adds at least one node in the already defined independent paths. In other words, the independent paths of a program are all unique.

## Steps for basis path testing

To conduct basis path testing of a program, follow the steps below:

1. Draw the control flow graph of the program.
2. Calculate the cyclomatic complexity of the control flow graph. This will be the maximum number of independent paths in the graph.
3. Identify independent paths in the control flow graph.
4. Design test cases based on the independent paths identified so that the test cases execute all independent paths.

## Advantages of basis path testing

The advantages of conducting basis path testing are:

• Basis path testing reduces the number of redundant tests.
• All program statements are executed and tested at least once.
• It guarantees complete branch coverage.

## Example

Consider the code snippet below, for which we will conduct basis path testing:

int num1 = 6;
int num2 = 9;
if(num2 == 0){
cout<<"num1/num2 is undefined"<<endl;
}else{
if(num1 > num2){
cout<<"num1 is greater"<<endl;
}else{
cout<<"num2 is greater"<<endl;
}
}

### Step 1: Draw the control flow graph

The control flow graph of the code above will be as follows:

### Step 2: Calculate cyclomatic complexity

The cyclomatic complexity of the control flow graph above will be:

where,

• E = The number of edges in the control flow graph.
• N = The number of nodes in the control flow graph.
• P = The number of connected components in the control flow graph.

### Step 3: Identify independent paths

The independent paths in the control flow graph are as follows:

• Path 1: 1A-2B-3C-4D-5F-9
• Path 2: 1A-2B-3C-4E-6G-7I-9
• Path 3: 1A-2B-3C-4E-6H-8J-9

### Step 4: Design test cases

The test cases to execute all paths above will be as follows:

 Path Input values Path 1: 1A-2B-3C-4D-5F-9 num1 = 9num2 = 0 Path 2: 1A-2B-3C-4E-6G-7I-9 num1 = 4num2 = 2 Path 3: 1A-2B-3C-4E-6H-8J-9 num1 = 6num2 = 8

RELATED TAGS

software testing

CONTRIBUTOR

Kainat Asif