We are going to break down the question into parts:
- Definition of code coverage.
- Different types of coverage metrics (line, branch, function, etc.)
- Reasonable code coverage target for a project.
- Can you have 100% code coverage and still have bugs? (Yes, and explain why)
- Explain why coverage is not a perfect measure of test quality.
Let's address each part.
Code Coverage: Definition, Metrics, and Limitations
What is Code Coverage?
Code coverage is a software testing metric that measures the proportion of a program's source code that is executed when a test suite runs. It quantifies how much of the codebase is exercised by automated tests, helping teams assess test effectiveness and identify untested areas. Coverage is typically expressed as a percentage.
Types of Coverage Metrics
Different metrics evaluate various aspects of code execution:
| Metric | What It Measures | Example |
|---|
| Line Coverage | Percentage of executable source code lines executed. | If 80 out of 100 lines run during tests → 80% coverage. |
| Branch/Decision Coverage | Percentage of control-flow branches (e.g., if/else, switch cases) taken. | For if (x > 0), tests must cover both true and false outcomes. |
| Function Coverage | Percentage of functions/methods invoked at least once. | If 9 of 10 functions are called → 90% function coverage. |
| Path Coverage | Percentage of unique execution paths through a function (combinatorial). | Hard to achieve for complex logic due to path explosion. |
| Condition Coverage | Percentage of boolean sub-expressions (conditions) evaluated to true and false. | For if (A && B), tests must cover A=true/false and B=true/false. |
| MC/DC (Modified Condition/Decision Coverage) | Used in safety-critical systems (e.g., aviation). Each condition shown to independently affect the decision outcome. | Ensures rigorous testing of complex logic. |
Reasonable Code Coverage Targets
Targets vary by project context:
- General Projects: 70–90% line/branch coverage is common. Lower tiers (≤50%) risk critical gaps; >90% may yield diminishing returns.
- Critical Systems (medical, aerospace): Often require 90–100% MC/DC or path coverage due to regulatory standards (e.g., DO-178C).
- Legacy Code: Start with measurable improvements (e.g., from 30% to 60%) if full coverage is impractical.
- Test Type: Unit tests should have higher coverage (e.g., 80%+), while integration/E2E tests focus on workflows, not exhaustive line coverage.
Key Insight: Coverage is a proxy for test thoroughness—not a guarantee. Prioritize testing critical logic, error paths, and complex conditions over chasing arbitrary percentages.
Can 100% Coverage Still Have Bugs? Yes.
Why? Coverage only tracks execution, not validation. Bugs persist because:
- Missing Assertions: Code runs, but tests may not verify correct outputs/state (e.g., testing
calculateTax() but not checking if result matches tax laws).
- Incorrect Test Oracles: Tests may validate wrong expectations (e.g., testing
add(2, 2) but asserting 5 due to a flawed assertion).
- Uncovered Edge Cases: 100% line coverage may still skip rare inputs (e.g., null values, overflow) if tests use typical data only.
- Concurrency/State Issues: Race conditions, deadlocks, or timing bugs might not manifest in test runs despite full coverage.
- Environment/Configuration: Tests pass in a controlled env but fail in production due to OS/hardware differences.
- Specification Gaps: Code may correctly implement wrong requirements (tests pass but business logic is flawed).
Why Coverage Is Not a Perfect Measure of Test Quality
- Quantity ≠ Quality: High coverage doesn't mean tests are meaningful—they might be trivial (e.g., testing getters/setters) or lack assertions.
- False Sense of Security: Teams may prioritize coverage numbers over designing tests for real user scenarios.
- Blind Spots:
- Coverage tools struggle with dynamic languages, reflection, or external dependencies.
- May not measure coverage of error-handling (e.g., exceptions in
catch blocks if triggered by mocks).
- Test Redundancy: Multiple tests could execute the same code paths without adding new checks.
- Ignores Test Design: Doesn’t reflect techniques like boundary-value analysis, equivalence partitioning, or pairwise testing.
- Maintenance Overhead: Chasing 100% coverage can lead to brittle, over-mocked tests that break with refactoring.
Best Practices
- Combine Metrics: Use line/branch coverage alongside mutation testing (which checks if tests detect injected faults).
- Focus on Risk: Target coverage toward high-impact code (security, business logic).
- Review Test Quality: Assess assertion strength, edge-case coverage, and readability.
- Automate & Integrate: Include coverage in CI pipelines but discuss trends, not just thresholds.
Conclusion
Code coverage is a useful diagnostic tool—not a goal. A project with 70% well-designed tests may be more robust than one with 100% superficial tests. Aim for meaningful coverage that validates behavior, not just execution.