liveBook · Manning

Code coverage

As explained in Shipping Go: Develop, deliver, discuss, design, and go again

Test coverage is a measure of how much of a system's code or behavior is exercised by tests.

A passing test suite is reassuring, but it does not show which parts of the code the tests never reach. Code coverage reports the percentage exercised by tests and highlights missed areas. Use it to find the next useful test, not as proof that the product works as intended. section 3.7

A practical coverage loop is:

  • Run the tests with coverage enabled. In Go, `go test ./... -cover` gives a quick view across packages. section 3.7
  • Inspect the missed code. Look especially for logic branches and error conditions that deserve tests; consider removing code that is no longer used. section 3.7
  • Add tests that check the code’s interface and expected behavior, then measure again. High coverage is useful, but 100% is not required. section 3.7 section 3.8

Make coverage visible

A Makefile can have `make test` write `coverage.out`, `make coverage` check the total, and `make report` create `cover.html`. The illustrated check uses 80% as a gate. Open the HTML report to locate tested and untested lines, then choose a meaningful missing case rather than writing a test solely to raise the number. section 3.7

Listing 3.22 Makefile
test:
    go test ./... -coverprofile=coverage.out              #1
 
coverage:
    go tool cover -func coverage.out | grep "total:" | \
    awk '{print ((int($$3) > 80) != 1) }'                 #2

The Makefile listing shows how the test target creates a Go coverage profile and a separate target checks its total. section 3.7

Run the check in continuous integration so developers receive the same feedback on each push. The illustrated pipeline runs tests, checks coverage, generates an HTML report, and archives that report. Keep watching test duration as the suite grows: coverage is one signal alongside whether unit, acceptance, and integration tests give timely feedback before release. section 3.7 section 9.5

How Shipping Go: Develop, deliver, discuss, design, and go again covers it

How deep it goes
Defines the percentage, demonstrates Go reports and a Makefile gate, and cautions that full coverage cannot establish correctness.
Examples
Uses Go coverage commands, a Makefile threshold check, an HTML report, and a CI pipeline that archives the report.
What it assumes
Assumes an existing Go test suite and introduces coverage measurement without requiring a 100% target.
What you can do after
Apply it via Go coverage tools to find missed code, add useful tests, and enforce a coverage check in CI.
How it connects
Connects to automated testing, refactoring, continuous integration, and test-suite feedback before release.

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 Shipping Go: Develop, deliver, discuss, design, and go again. The section links point to the parts of the book it draws on.