liveBook · Manning

Go test

As explained in Go by Example

go test is the Go command that builds and runs tests for Go packages.

go test builds and runs Go package tests, reports their results, and removes the compiled test binary afterward. It also supports benchmarks and profiling. section 1.7 section 2.3.2

Use go test to:

  • Run package tests such as `go test ./url`. section 2.3.2
  • Rerun tests with `-count=1` when cached results should be skipped. section 2.3.2
  • Display individual test names and logs with `-v`. section 2.3.2
  • Shuffle test order with `-shuffle=on`, then reproduce an order with its seed. section 2.3.2

Run a package test

From the Go module directory, run `go test ./url`. A passing run reports `PASS` and the package path; a repeated run may report `(cached)`. Add `-v` to inspect individual tests such as `TestParse` and `TestURLString`.

Figure 2.2 The go test tool and the testing package collaborate on running tests. Tests get a *T, which has methods for tests to call, log, report failures, and so on.
A close-up of a phone

AI-generated content may be incorrect.

Shows how go test collaborates with the testing package: test functions receive *testing.T for logging and failure reporting. section 2.3

How Go by Example covers it

How deep it goes
Explains compilation, result caching, verbose output, and shuffle seeds, but defers detailed benchmark and profiling procedures.
Examples
Uses the url package with TestParse and TestURLString, plus cached, verbose, shuffled, and seed-reproduced command output.
What it assumes
Assumes Go package and module familiarity while teaching test-file naming, Test functions, and *testing.T conventions.
What you can do after
Operate go test to run, inspect, rerun, shuffle, and reproduce Go package tests from the module directory.
How it connects
Connects testing package, test files, subtests, table-driven tests, benchmarks, profiling, and Go modules.

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 by Example. The section links point to the parts of the book it draws on.