liveBook · Manning

Code coverage

As explained in Unit Testing Principles, Practices, and Patterns

The ratio of production-code lines executed by at least one test to the total number of production-code lines.

Code coverage measures the ratio of production-code lines executed by at least one test to the total number of production-code lines. It provides useful feedback about what tests reach, but it cannot measure test-suite quality by itself. section 1.3

Figure 1.3. The code coverage (test coverage) metric is calculated as the ratio between the number of code lines executed by the test suite and the total number of lines in the production code base.

Use the ratio diagram to distinguish executed production-code lines from the total code base. section 1.3

A partially covered method

A test calling IsStringLong("abc") executes the false path and verifies the result. The test therefore reaches some method lines, while the longer-string path remains untested. A test with no assertions could execute both paths yet provide no evidence that either behavior is correct. section 1.3

Interpret coverage numbers as feedback rather than a quality target:

  • Treat low coverage as a warning that important code may be insufficiently tested. section 1.3
  • Do not treat high or complete coverage as proof of a good test suite. section 1.3
  • Prefer valuable tests for important code over imposing a universal coverage threshold; branch coverage offers more insight but still omits assertions and third-party paths. section 1.6

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Defines the line-ratio calculation and contrasts its negative signal with its inability to guarantee test quality.
Examples
Uses IsStringLong tests, an assertion-free test, and a partially covered conditional method.
What you can do after
Evaluate test reach and detect likely under-testing without treating a coverage number as a quality guarantee.
How it connects
Links code coverage to test suites, branch coverage, assertions, unit testing, and third-party libraries.

Also explained in

Compare how 14 books explain code coverage →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Unit Testing Principles, Practices, and Patterns. The section links point to the parts of the book it draws on.