Skip to content
Latchkey

What Is Code Coverage?

Code coverage is the family of metrics that report how much of your source code ran during a test execution.

Code coverage and test coverage are often used interchangeably, but "code coverage" usually refers to the specific measurement types, line, branch, function, and statement, and how a tool computes them. Understanding the types helps you read a coverage report without being misled by a single headline number.

The common coverage types

  • Line or statement coverage: which lines executed.
  • Branch coverage: which true/false paths of each decision ran.
  • Function coverage: which functions were called.
  • Condition coverage: which boolean sub-expressions were evaluated each way.

How instrumentation works

A coverage tool inserts counters into your code, either by rewriting the source, transforming bytecode, or using a runtime hook. As the tests run, those counters tick. Afterward the tool compares what ran against the total to produce percentages and a line-by-line map.

What the numbers do and do not mean

A high line-coverage number can hide untested branches, so branch coverage is usually the more honest signal. None of the numbers prove your assertions are correct; they only prove the code ran. Read coverage as a map of blind spots, not a grade.

A quick example

A function with an if/else can hit 100 percent line coverage while only one branch is exercised, which is why branch coverage often tells a truer story.

Lines run vs branches missed
function classify(n) {
  if (n > 0) return "positive"; // tested
  return "non-positive";        // line never run
}

Code coverage in CI

Pipelines typically generate coverage in a standard format such as LCOV or Cobertura, then upload it to a dashboard that tracks the trend and comments on pull requests. The extra instrumentation slows tests somewhat, so running on fast runners keeps the coverage step from dominating the build.

Key takeaways

  • Code coverage spans line, branch, function, and condition metrics.
  • Tools instrument the code to record what ran during the tests.
  • Branch coverage is usually a more honest signal than line coverage.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →