Go CLI Testing and Benchmarks

go test patterns for package tests, focused runs, benchmarks, fuzzing, coverage, JSON output, and CI-friendly test workflows.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Test Basics
Test all packages
go test ./...

# Run tests for all packages in the module.

Verbose tests
go test -v ./...

# Print each test as it runs.

Run matching tests
go test -run TestHTTP ./...

# Run only tests whose names match a regexp.

Disable test cache
go test -count=1 ./...

# Force tests to run instead of using cached results.

Stop on first failure
go test -failfast ./...

# Abort package test execution after the first failure.

## Benchmarks
Run benchmarks
go test -bench=. ./...

# Run benchmarks matching a regexp.

Set benchmark time
go test -bench=. -benchtime=3s ./...

# Control how long each benchmark runs.

Show alloc stats
go test -bench=. -benchmem ./...

# Include memory allocation stats in benchmark output.

Benchmark specific package
go test -run=^$ -bench=BenchmarkParse .

# Benchmark current package only.

## Coverage
Cross-package coverage
go test -coverpkg=./... ./...

# Measure coverage for multiple packages while testing one package set.

Write coverage profile
go test -coverprofile=coverage.out ./...

# Write a cover profile for later analysis.

Summarize coverage file
go tool cover -func=coverage.out

# Show function-by-function coverage from a profile.

Open HTML coverage
go tool cover -html=coverage.out

# Render an HTML coverage report.

## Fuzzing
Run fuzz target
go test -fuzz=FuzzParse

# Run or continue fuzzing for a matching fuzz target.

Limit fuzz duration
go test -fuzz=FuzzParse -fuzztime=30s

# Run fuzzing for a bounded amount of time.

Run fuzz seed corpus
go test -run=FuzzParse

# Run only seed inputs in a fuzz test.

## CI and Machine Output
Emit JSON test events
go test -json ./...

# Generate machine-readable test events.

Shuffle test order
go test -shuffle=on ./...

# Detect order-dependent tests by shuffling.

Set timeout
go test -timeout=2m ./...

# Fail tests that exceed a timeout.

Limit parallel tests
go test -parallel=4 ./...

# Set max parallel tests in a package.

Recommended next

No recommendations yet.