Software Testing - Code Coverage and Test Coverage Metrics

Code coverage and test coverage are quantitative metrics used to measure how much of the source code is executed when test cases run.
They help evaluate the thoroughness of testing, not the correctness of the software.

High coverage ≠ defect-free software
Low coverage = untested code (high risk)


1. Code Coverage vs Test Coverage

Aspect Code Coverage Test Coverage
Focus Source code execution Test cases vs requirements
Measures Executed code elements Requirement validation
Used by Developers & testers Test managers
Tools JaCoCo, Istanbul, Coverage.py Test traceability matrices

2. Statement Coverage

Statement coverage measures the percentage of executable statements that are executed at least once.

Formula

Statement Coverage (%) =
(Executed statements / Total executable statements) × 100

Example

if (x > 0) {
   y = 10;
}
z = 5;
  • Total statements: 3

  • Executed when x > 0: all 3 → 100% coverage

  • Executed when x ≤ 0: 2 → 66% coverage

Characteristics

  • Easy to measure

  • Ensures every line runs at least once

  • Does not test decision outcomes fully


3. Branch Coverage (Decision Coverage)

Branch coverage measures whether each possible branch (true/false) of decision points is executed.

Formula

Branch Coverage (%) =
(Executed branches / Total branches) × 100

Example

if (x > 0) {
   y = 10;
} else {
   y = 20;
}
  • Total branches: 2 (true, false)

  • If only x > 0 is tested → 50% coverage

  • Both paths tested → 100% coverage

Characteristics

  • Stronger than statement coverage

  • Detects missing logic paths

  • Still does not test all combinations


4. Path Coverage

Path coverage measures whether all possible execution paths through the program are tested.

Example

if (a > 0) {
   if (b > 0) {
      x = 1;
   }
}

Possible paths:

  1. a ≤ 0

  2. a > 0, b ≤ 0

  3. a > 0, b > 0

All must be tested for 100% path coverage.

Characteristics

  • Most thorough coverage metric

  • Detects complex logical errors

  • Often impractical for large programs due to path explosion


5. Other Common Coverage Metrics

Function / Method Coverage

  • Measures whether each function is called at least once

Condition Coverage

  • Ensures each boolean condition evaluates to both true and false

Condition-Decision Coverage

  • Combines branch and condition coverage


6. Why Coverage Metrics Are Important

  • Identify untested code

  • Improve test completeness

  • Support risk-based testing

  • Provide objective testing progress data

  • Improve confidence before release


7. Limitations of Coverage Metrics

  • Cannot guarantee correctness

  • Cannot detect missing requirements

  • High coverage can still miss defects

  • Focuses on execution, not validation


8. Coverage Metrics in Practice

Coverage metrics are commonly used in:

  • Unit testing

  • Regression testing

  • Continuous integration (CI/CD)

  • Code quality gates

Typical industry targets:

  • Statement coverage: 70–80%

  • Branch coverage: 60–75%

  • Path coverage: selective (critical logic only)

  • Statement coverage → lines executed

  • Branch coverage → decision outcomes executed

  • Path coverage → execution paths tested

  • Coverage measures extent, not effectiveness