November 14, 2019
Basis Path Testing – A White Box Method for Designing Test Cases
In this article, we will uncover what basis path testing means, and outline the advantages of using this white box method for a program’s source code.
In software engineering, it is critical to have good test coverage to ensure the quality of the product you are delivering. Basis path testing uses a white box testing methodology for designing test cases based on the logical path or flow through a program.
Similar to path testing, basis path testing finds all the possible executable paths of the code. However, it is more efficient in that it reduces redundant tests and achieves maximum coverage, and is, therefore, less time-consuming.
Understanding A White Box Testing Method
The Box Testing approach to testing software is made up of two methods, namely White Box and Blackbox testing. To give an overview, the white box method tests software’s internal structure, coding, and design while the black box method tests it from an end-user or external viewpoint.
White box testing is known by a number of different terms including Clear Box testing, Structural testing, Open Box testing, Code-Based testing, Transparent Box testing, and Glass Box testing. It earned this name because of the tester’s ability to cut through the outer box of the software to see into its inner workings. The code is visible to the developer.
Black box testing, on the other hand, is symbolic of not being able to see inside the inner workings, leaving only the end-user experience to be tested. For the purposes of this article, we will focus more closely on the former.
The main objective of white-box testing is to verify input and expected output flows through an application, refine the usability, improve on the design, and strengthen security. It can be done at either system, integration, or unit levels of the development phase.
When implemented, this method aims to validate the flow of processes for an application. Using a set of predefined inputs and desired outputs, testing is performed to identify problems with the code. When a specific input fails to produce the expected output, a bug is encountered.
Defining Path Testing
In order to know about basis path testing, it is necessary to understand path testing. The term Path refers to the flow of execution or sequence of commands and conditions in a definitive direction. In other words, it is the route that a process takes from one particular point to another. An independent path adds at least one new process, command, or condition to the already defined independent paths.
Every software program has multiple entry and exit points in the source code. Path testing verifies these points in the source code to ensure there are no bugs encountered in the execution of processes through a program sequence. The method can be designed to either execute all or selected paths through a program.
The challenge with this arrives in complex programs where the entry and exit points to be tested total up. Depending on the amount of source code to be tested, this can take days or even weeks. This is where basis path testing is useful as it reduces the total test cases needed.
What Is Basis Path Testing?
Through utilizing a white box method, basis path testing can attain maximum path coverage using the minimum number of test cases.
Every possible block of code in a program is executed through the lowest number of test cases. It does this by identifying the number of independent paths so that the number of test cases required can be explicitly defined, thus maximizing the coverage of each test case.
Basis path testing is effective because it ensures full branch coverage without needing to cover all the possible paths. As already mentioned, this can be time-consuming and costly. Branch coverage is another testing method that aims to verify that every branch extending from every decision point is tested at least once. This way, all the branches in the code can be validated to make sure that none result in the application behaving abnormally. It so happens then that, basis path testing is considered to be a hybrid of path and branch testing methods.
Some of the main points to consider for this method include:
- White box testing technique
- Often used by developers at the unit level to test code of a software application
- Structural testing method for a program’s source code
- Helps to evaluate internal workings of an application
- Uses cyclomatic complexity to calculate the number of tests to be executed
- Defines separate test cases to be executed for each independent path
- Provides full branch coverage when evaluating source code
To illustrate how to implement the steps of basis path testing, we have included an example. Below is a flow diagram showing nodes for logical paths, statements, and conditionals changing the flow of execution.
This provides a simple example of what basis path testing looks like. There are a number of conditional statements that are executed depending on input parameters. In this case, there are 3 paths or conditions to be tested to determine the output:
Path 1: 1 2 3 5 6 7
Path 2: 1 2 4 5 6 7
Path 3: 1 6 7
Steps For Carrying Out Testing
As an overview, the steps for carrying out this testing method includes:
- Drafting a control flow graph to identify the possible program paths
- Calculating the number of independent paths through a process known as cyclomatic complexity which we discuss below
- Define the set of basis paths to be tested
- Generate test cases to evaluate the program flow for each path
Cyclomatic complexity is a software metric and another key process in implementing basis path testing. A software metric is a quantitative measurement of time, quality, size, and cost of an attribute of software.
In this case, cyclomatic complexity measures the complexity of a program by identifying all independent paths through which the processes flow.
The metric is based on a control flow representation of a program and was developed in 1976 by Thomas McCabe. His model uses a flow graph that consists of nodes and edges to present a visualization of the control flow of a program. Nodes symbolize the processing tasks and edges control flow between the nodes. Nodes are the entry and exit points of processes in the program sequence while independent paths add a new process to the program flow. They have at least one edge which has not been followed in any other paths.
A mathematical representation of the cyclomatic complexity of program code can be calculated as follows:
V(G) = E – N + 2
E = number of edges
N = number of nodes
V(G) = P + 1
P = number of predicate nodes (nodes that contain conditions)
Once the number of paths or conditions has been calculated, the number of tests to be written is known. For example, 3 paths will mean that at least one test should be generated to cover each path.
The properties of cyclomatic complexity are as follows:
- V(G) is the highest number of independent paths shown in the graph
- V(G) is always greater than or equal to 1
- If V(G) is equal to 1 then G will have one path
- Ideally, minimize the complexity score to 10 – the higher the score, the more complex the code
Designing Test Cases
As basis path testing is a white box method for designing test cases and deals with the inner workings of the code, it is important that the tester understands the source code of the application before designing test cases.
Not only should the testers be knowledgeable in the programming languages used in the application, but they should also be proficient in secure coding practices. Security is often a driving factor for testing software, and the tester should know how to identify any code at risk of malicious attack.
Moving onto designing test cases, testers can write more code to test the source code of the application for flow and structure. With independent branches of code isolated by basis path testing, writing and executing code to test the flow of processes is significantly simplified.
Using this white box technique guarantees to execute at the least, one statement during testing. Each independent linear path within the program is checked, which means the number of test cases will equal the cyclomatic complexity score of the program.
Advantages Of Basis Path Testing
- Reduces the number of redundant tests
- As it is one of the core white box testing methods, it helps to validate other white-box techniques
- Focuses test cases on program logic
- Facilitates analytical test case design over arbitrary test case design
- Program statements will be executed at least once for test cases exercising basis set
Basis path testing identifies independent paths in source code through which software execution flows. The main objective of this testing method is to ensure that every path is covered and executed. It is also designed to reduce the occurrence of redundant tests.