Software Engineering basics - Path Testing in Software Engineering

Path Testing in Software Engineering

Definition

Path Testing is a white-box testing technique used to ensure that all possible execution paths in a program are tested at least once.

It focuses on the control flow of the program — that is, the sequence in which statements, decisions, and loops are executed.


Purpose of Path Testing

  • To detect logic errors in the code.

  • To ensure complete coverage of different execution paths.

  • To validate that all decisions and conditions in the code behave correctly.

  • To improve the reliability and quality of the software.


Key Concepts

  1. Control Flow Graph (CFG):
    A diagram representing the flow of control in a program.

    • Nodes represent statements or blocks of code.

    • Edges represent the flow of control between them.

  2. Path:
    A sequence of statements executed from the program’s start to end.

  3. Independent Path:
    A path that introduces at least one new edge (or condition) not covered by previous paths.

  4. Basis Path Testing:
    A structured form of path testing that ensures minimum number of test cases to cover all independent paths.


Steps Involved in Path Testing

Step No. Activity Description
1 Draw Control Flow Graph (CFG) Convert the source code into a flowchart-like graph showing all decisions, loops, and paths.
2 Identify Independent Paths Determine unique paths through the program logic (each introducing a new edge).
3 Calculate Cyclomatic Complexity Use the formula V(G) = E – N + 2, where E = number of edges and N = number of nodes. This tells you the number of independent paths to test.
4 Design Test Cases Create input values that will execute each independent path at least once.
5 Execute Test Cases Run the designed test cases and verify the output.
6 Compare Results & Report Compare actual results with expected results, and log defects if any path fails.

Diagram: Path Testing (Example)

Here’s a simple example and its corresponding flow:

Code Example:

if A > 0:
    if B > 0:
        print("Path 1")
    else:
        print("Path 2")
else:
    print("Path 3")

Control Flow Graph:

        [Start]
           │
           ▼
       [Check A>0]
        /       \
      Yes         No
     /              \
[Check B>0]         [Path 3]
   /     \
Yes       No
 |         |
[Path 1] [Path 2]

Possible Paths:

  1. Start → A>0 (True) → B>0 (True) → Path 1

  2. Start → A>0 (True) → B>0 (False) → Path 2

  3. Start → A>0 (False) → Path 3

Each of these paths represents an independent path that should be tested.


Cyclomatic Complexity Calculation

For the above CFG:

  • Nodes (N) = 6

  • Edges (E) = 7
    Formula:
    [
    V(G) = E - N + 2 = 7 - 6 + 2 = 3
    ]

So, 3 independent paths need to be tested.


Advantages of Path Testing

  • Ensures logical completeness of the code.

  • Identifies unreachable code or redundant paths.

  • Useful for testing complex decision structures.

  • Helps improve code quality and reliability.


Disadvantages

  • Becomes complex for large programs.

  • Not suitable for black-box testing (since it requires code access).

  • Requires technical expertise to analyze the control flow graph.


Summary

Aspect Description
Type White-box testing
Focus Logical paths and decision structures
Goal Cover all independent paths
Main Tool Control Flow Graph (CFG)
Metric Used Cyclomatic Complexity