liveBook · Manning

Code coverage

As explained in gRPC Microservices in Go

Code coverage is a metric indicating the proportion of source-code elements exercised by a set of tests.

Code coverage measures how much production code a test suite exercises. Its practical use is to find code that still needs tests. In a microservice, run coverage alongside automated tests so changes to a service can be checked without manually testing the entire system. section 7.5 section 7.0

Use Go’s built-in tools to move from a package percentage to a report you can inspect:

  • Run `go test -cover ./...` to see coverage for each package with its test result. The reported percentage is statement coverage. section 7.5
  • Run `go test ./... -cover -coverprofile=coverage.out` to save coverage data. Then run `go tool cover -html=coverage.out` to inspect its distribution across files and functions. section 7.2.4 section 7.5.1
  • Pass `coverage.out` to CI checks on pull requests. A configured threshold can fail a check when coverage drops, prompting another look at the tests. section 7.5.1 section 7.5.2

Coverage for an Order service

Tests of `PlaceOrder` can mock database and payment calls, checking a successful order, a persistence error, and a payment error. Run coverage after these tests to see how much of the package they exercise and where further tests are needed. A percentage is a guide to exercised code, not a substitute for checking the behavior each test asserts. section 7.2.4 section 7.5

ok      
 github.com/huseyinbabal/microservices/order/internal/application/core/a
 pi       0.274s  coverage: 93.3% of statements

The sample Go test result reports 93.3% statement coverage for one package. section 7.5

How gRPC Microservices in Go covers it

How deep it goes
Walks through package percentages, saved profiles, HTML inspection, and CI checks without detailing coverage instrumentation.
Examples
Uses mocked Order-service tests and a Go package result reporting 93.3% statement coverage.
What it assumes
Assumes runnable Go tests and packages; introduces the commands for generating and inspecting coverage.
What you can do after
Operate/configure it with Go test flags, inspect a coverage profile, and feed it into CI checks.
How it connects
Connects unit testing and mocks to package-level reporting, pull-request checks, and continuous integration.

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 gRPC Microservices in Go. The section links point to the parts of the book it draws on.