What is code coverage, and is 100% coverage a good goal?
BeginnerConceptTesting in DevOps
Answer
In one line: Code coverage is the percentage of your code that runs during automated tests; it helps find untested code, but 100% coverage is usually not a good goal because high coverage doesn't guarantee good tests.
Types of coverage:
- Line coverage — which lines were executed.
- Branch coverage — whether both sides of every
if/elsewere tested (more meaningful). - Function coverage — which functions were called.
Tools: JaCoCo (Java), coverage.py / pytest-cov (Python), Istanbul / nyc / Jest (JavaScript), Go's go test -cover; results shown in SonarQube or Codecov.
Why 100% isn't the right target:
- Coverage ≠ correctness — a test can run a line without checking its result.
calculateTax(100)executed but never compared to the expected value still counts as covered. - Diminishing returns — the last 10–20% (simple getters, generated code, framework glue) costs a lot of effort for little value.
- Bad incentives — chasing a number leads to useless tests written just to hit it.
What works better:
- A reasonable threshold — often 70–80% — focused on business logic.
- Coverage on new code (for example "new code must have 80% coverage"), so quality improves over time without blocking work on legacy code.
- Quality of assertions — tests should check real behaviour and edge cases.
- Mutation testing (Stryker, PIT) to check if tests actually catch bugs: it changes the code slightly and sees if a test fails.
Example quality gate: SonarQube fails the PR if coverage on new code is below 80%, but doesn't block on overall coverage of the old codebase.
Tip for interviews: say "coverage is a useful signal, not a goal" and mention coverage on new code.
More on Testing in DevOps
What is the testing pyramid?BeginnerWhat is the difference between unit, integration and end-to-end tests?BeginnerWhat are smoke tests and sanity tests?BeginnerWhat is regression testing?BeginnerWhat is test automation, and what should (and shouldn't) be automated?BeginnerWhat is shift-left testing, and how do you apply it in a pipeline?Intermediate