Experiment 9: Estimation of Test Coverage Metrics & Structural Complexity
← Back to Index
To calculate test coverage metrics and cyclomatic complexity for a given program module.
McCabe's Cyclomatic Complexity
Cyclomatic Complexity V(G) measures the number of linearly independent paths through a program.
$$V(G) = E - N + 2P$$
Symbol
Meaning
E
Number of edges in the Control Flow Graph (CFG)
N
Number of nodes in the CFG
P
Number of connected components (usually 1)
Alternate formula:
$$V(G) = \text{Number of decision points} + 1$$
Complexity Interpretation
V(G) Value
Risk Level
Recommendation
1–10
Low
Simple, well-structured code
11–20
Moderate
Moderate risk, review recommended
21–50
High
Complex, hard to test
> 50
Very High
Untestable, must refactor
Metric
Formula
Meaning
Statement Coverage
(Executed Statements / Total Statements) × 100
% of code lines executed
Branch Coverage
(Covered Branches / Total Branches) × 100
% of true/false paths taken
Path Coverage
(Executed Paths / Total Paths) × 100
% of distinct paths covered
Condition Coverage
(Evaluated Conditions / Total Conditions) × 100
% of Boolean sub-expressions evaluated
Coverage Strength: Path > Branch > Statement > Condition
Take the given program/function and draw its Control Flow Graph (CFG) .
Identify nodes (statements/blocks) and edges (control flow) in the CFG.
Count total edges (E) , nodes (N) , and connected components (P) .
Calculate Cyclomatic Complexity: V(G) = E - N + 2P.
Verify using: V(G) = Number of decision points + 1.
Identify all independent paths (basis paths) — count equals V(G).
Design test cases to cover each independent path.
Calculate Statement Coverage after test execution.
Calculate Branch Coverage after test execution.
Prepare coverage report table.
Program: Grade Calculator
def getGrade (marks ): # Node 1
if marks >= 90 : # Node 2 (Decision 1)
return 'A' # Node 3
elif marks >= 75 : # Node 4 (Decision 2)
return 'B' # Node 5
elif marks >= 60 : # Node 6 (Decision 3)
return 'C' # Node 7
else :
return 'F' # Node 8
Control Flow Graph Analysis
N1 (start)
↓
N2 [marks >= 90?] —True→ N3 (return 'A') → END
↓ False
N4 [marks >= 75?] —True→ N5 (return 'B') → END
↓ False
N6 [marks >= 60?] —True→ N7 (return 'C') → END
↓ False
N8 (return 'F') → END
Metric
Value
Nodes (N)
8
Edges (E)
10
Connected Components (P)
1
V(G) = E - N + 2P
10 - 8 + 2 = 4
Decision Points + 1
3 + 1 = 4 (verified)
Independent Paths (Basis Paths)
Path
Condition
Test Case
Expected
Path 1
marks >= 90
marks = 95
'A'
Path 2
75 ≤ marks < 90
marks = 80
'B'
Path 3
60 ≤ marks < 75
marks = 65
'C'
Path 4
marks < 60
marks = 45
'F'
Metric
Calculation
Result
Statement Coverage
4 branches / 4 total × 100
100%
Branch Coverage
8 branches covered / 8 total × 100
100%
Path Coverage
4 paths / 4 total × 100
100%
TC
Input
Expected
Actual
Status
TC1
marks = 95
'A'
TC2
marks = 80
'B'
TC3
marks = 65
'C'
TC4
marks = 45
'F'