https://unsplash.com/photos/lu15z1m_KfM?utm_source=unsplash&utm_medium=referral&utm_content=creditShareLink

Basis Path Testing

Ryan Craven

--

Basis path testing is a powerful software testing technique. Basis path testing is an excellent technique for new software testers or developers to learn because anyone can visually see the testing paths, requiring minimal testing knowledge. There’s one dependency that I want to call out, though, this testing is only as good as the control-flow graph; if the graph is wrong, the testing will be inaccurate.

In this article, I will cover:

  • What is basis path testing
  • Cyclomatic Complexity
  • Determining basis paths
  • Example

Basis path testing is a white box testing technique where tests are designed based on paths through the application rather than decisions. This type of testing is best for critical parts of the software. It allows maximum coverage while reducing the number of tests needed to test the application. Using basis path testing also helps to eliminate redundant testing. However, before diving into basis path testing more, we first need to understand cyclomatic complexity.

Cyclomatic complexity was developed by Thomas McCabe and is used to measure software complexity. To determine the complexity, you’ll first need to draw a control-flow graph of the application. Once the graph is drawn out, cyclomatic complexity can be determined using this equation:

M = L-N + 2P, where L = number of edges (or links), N = number of nodes, and P = number of disconnected parts of the chart (subroutines).

M from the equation has a couple of meanings:

  1. The higher the number, the more complicated the software and the greater the risk. Therefore, if the software has a cyclomatic complexity greater than 10, developers may need to look for ways to simplify the logic.
  2. The number indicates the minimum number of tests needed for maximum path coverage.

Steps for Determining Basis Paths

  1. Draw the control-flow graph for the application under test
  2. Calculate the cyclomatic complexity (This will indicate how many tests are required for 100% path coverage)
  3. Determine the MOST critical path through the graph using risk base analysis — This will be the baseline path (should not be an exception path)
  4. Generate another path by changing the outcome of the baseline path
  5. Continue to generate a new path by changing the outcome of the baseline path until all paths have been covered
  6. Once all paths are generated, the total number of paths should equal the cyclomatic complexity.
  7. Create tests that follow each path

Example:

  1. Given the sudo code and control flow graph:
Example of a control-flow graph

2. Using the graph, the cyclomatic complexity is:

L-N + 2P (L = 15, N = 13, P = 1)
15–13 + 2 = 4

3. Lets pretend that the following path is the critical path (1):

4. We can generate three other paths (2,3,4) by changing the baseline decision path the least amount as possible

Looking at these paths, you’ll notice we minimized redundant testing and all paths have been covered. The only thing left to do is to write up or automate these tests cases for 100% path coverage.

Happy Testing!

To stay updated on future posts, subscribe to my newsletter below:

If you enjoy my articles, be sure to check out my Etsy Bookstore

--

--

Ryan Craven

Sr Quality Engineer & AI Enthusiast • Writing on AI, Tech & Testing • Read my articles for free and join my newsletter: https://ryancraventech.substack.com