liveBook · Manning

Go test

As explained in Go in Action, Second Edition

go test -cover is the Go test command mode that runs package tests and reports the percentage of statements they execute.

Tests are easier to maintain when they run the same way across projects. `go test` ships with Go and needs no separate test framework or configuration file. It compiles a package’s `*_test.go` files into a test executable, runs the tests, and reports the package result. By default, it tests the current directory and keeps successful output brief. section 1.6 section 8.2

Use the runner to narrow a failure or inspect what the tests exercise:

  • Name a test `TestXxx(t *testing.T)` in a `*_test.go` file. If no tests run, check the filename and function signature. section 8.1
  • Run `go test -v` to see passing tests and their logs; use `go test -run TestMemory` to select a matching test while debugging. section 8.2 section 8.5.4
  • Run `go test -cover` to report the percentage of package statements executed. For annotated source, run `go test -coverprofile=coverage.out`, then `go tool cover -html=coverage.out`. section 8.5.7

Coverage is a clue, not a pass

A calculator package reports 93.8% statement coverage. Adding a test for storing a result after an invalid calculation reaches the missed path, but `go test -cover` then reports 100% coverage alongside a failing `TestMemory`: the calculator retained an error and did not store the later valid result. Resetting that error fixes the behavior. Read the coverage percentage together with the test result; executing every statement does not mean the tests passed. section 8.5.7

The same runner supports `go test -bench=.` for benchmarks and `go test -race` to check concurrent code for race conditions during development. Use coverage to find gaps, then write assertions for the behavior those paths should have. section 8.7.1 section 9.6.1 section 8.8

Table 8.1 Types of test functions

Function name

Argument type

Type

Purpose

Example

TestXxx

*testing.T

Test

Test functionality with pass/fail status

func TestMyFeature(t *testing.T){}

BenchmarkXxx

*testing.B

Performance benchmark

Report performance data and enable profiling

func BenchmarkMethod(b *testing.B){}

FuzzXxx

*testing.F

Input fuzzing

Test inputs and detect edge cases

func FuzzMyInputs(f *testing.F){}

ExampleXxx

(none)

Testable example

Document usage and provide additional testing

func ExampleUsage(){}

The function-signature table distinguishes tests from benchmarks, fuzz targets, and testable examples—useful when a function is not being discovered. section 8.1

How Go in Action, Second Edition covers it

How deep it goes
Explains test discovery, execution, output, selection, and coverage reporting, including a worked coverage-driven bug fix.
Examples
Uses a passing logged test and a calculator recovery test that fails despite reaching 100% statement coverage.
What it assumes
Assumes Go package code; supplies the `*_test.go` naming rule, test function signature, and a module setup example.
What you can do after
Operate/configure it to run or select tests, inspect coverage, and investigate an uncovered calculator behavior.
How it connects
Connects test functions and statement coverage to benchmarks, fuzz testing, and race detection.

Also explained in

Compare how 4 books explain go test →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.