LinuxCommandLibrary

go-test

Test Go packages

TLDR

Test the package found in the current directory

$ go test
copy

[v]erbosely test the package in the current directory
$ go test -v
copy

Test the packages in the current directory and all subdirectories (note the ...)
$ go test -v ./...
copy

Test the package in the current directory and run all benchmarks
$ go test -v -bench .
copy

Test the package in the current directory and run all benchmarks for 50 seconds
$ go test -v -bench . -benchtime 50s
copy

Test the package with coverage analysis
$ go test -cover
copy

SYNOPSIS

go test [build flags] [-test.testpattern regexp] [packages] [test flags]

PARAMETERS

-v
    Verbose output: prints all tests as they are run

-run regexp
    Run tests matching the regular expression

-bench regexp
    Run benchmarks matching the regular expression

-benchmem
    Print memory allocation statistics for benchmarks

-benchtime t
    Run benchmarks for duration t (default 1s)

-count n
    Run each test and benchmark n times (default 1)

-parallel n
    Allow parallel execution of tests up to n

-timeout t
    Fail if test execution takes longer than t

-cover
    Enable coverage analysis

-coverprofile file
    Write coverage profile to file

-short
    Run shorter version of tests (if -short is supported)

-c
    Compile test binary but do not run it

-i
    Install dependencies before running tests

-o file
    Compile test binary to named file or directory

DESCRIPTION

go test is a command from the Go toolchain that automates testing for Go packages. It compiles the package with any requisite source files, then runs the resulting test binary. Tests are defined in files named package_test.go using the testing package.

By default, go test executes all tests matching the package name and prints a pass/fail summary like ok example.com/mypkg 0.123s or FAIL example.com/mypkg 0.456s. Use flags to control verbosity, select specific tests via regex, run benchmarks, generate coverage profiles, or perform other profiling tasks.

It supports running tests in parallel, setting timeouts, and more. For packages with subpackages, specify import paths explicitly. Benchmarks use BenchmarkXxx functions, examples use ExampleXxx. Integration with build flags allows custom compilation options. Essential for Go development workflows, ensuring code reliability before deployment.

CAVEATS

Tests must follow Go conventions (e.g., *_test.go files, TestXxx funcs). External test dependencies require build tags. Long-running tests may need adjusted -timeout. Coverage requires Go 1.2+. Not for non-Go code.

TEST FUNCTIONS

Define tests with func TestXxx(*testing.T), benchmarks func BenchmarkXxx(*testing.B), examples func ExampleXxx().

EXAMPLES

go test -v ./... (tests all packages recursively).
go test -bench=. -benchmem (run all benchmarks with mem stats).
go test -coverprofile=c.out && go tool cover -html=c.out (view coverage HTML).

HISTORY

Introduced in Go 1.0 (2009) as part of the Go toolchain. Evolved significantly: parallel testing in Go 1.2 (2013), coverage improvements in Go 1.4, race detector integration in Go 1.1, fuzzy test selection via globs in Go 1.10, and JSON output in Go 1.14. Usage standardized in Go development for CI/CD pipelines.

SEE ALSO

go(1), go build(1), go run(1), go vet(1)

Copied to clipboard