Code Coverage
A metric that measures the percentage of source code executed during automated testing.
Also known as: Test Coverage, Code Coverage Metric
Category: Software Development
Tags: testing, quality, software-engineering, metrics, automation
Explanation
Code Coverage is a software quality metric that quantifies how much of a program's source code is exercised by its test suite. It helps identify untested areas and serves as one indicator of test thoroughness.
Types of coverage:
1. **Line coverage** - Percentage of code lines executed
2. **Branch coverage** - Percentage of decision branches (if/else) taken
3. **Function coverage** - Percentage of functions called
4. **Statement coverage** - Percentage of statements executed
5. **Condition coverage** - Whether each boolean sub-expression has been evaluated to both true and false
6. **Path coverage** - Percentage of possible execution paths tested (often impractical for complex code)
Common thresholds:
- 80% is a widely used minimum target
- Critical systems (medical, aviation) may require 100% branch coverage
- Library/framework code often targets 90%+
- Legacy code may start lower with incremental improvement
Benefits:
- Identifies untested code paths
- Provides objective quality gate criteria
- Encourages writing tests alongside code
- Tracks test quality trends over time
Limitations:
- High coverage does not guarantee high quality — code can be covered without meaningful assertions
- Pursuing 100% coverage often yields diminishing returns
- Can incentivize writing low-value tests just to hit a number
- Does not measure test quality, only code execution
Popular tools: Istanbul/nyc (JavaScript), Coverage.py (Python), JaCoCo (Java), lcov (C/C++), SimpleCov (Ruby).
Best practice: Use coverage as a quality gate floor (not a ceiling), focus on meaningful test coverage of critical paths, and combine with mutation testing for deeper quality insights.
Related Concepts
← Back to all concepts