Benchmarks, Fuzzing and Coverage in Go
Measure speed with benchmarks, find edge cases with fuzzing, and see untested code with coverage reports.
Beyond pass and fail, go test can measure performance, search for inputs that break your code, and show which lines your tests never run.
Benchmarks
func BenchmarkJoin(b *testing.B) {
for b.Loop() {
join(parts)
}
}
Run with go test -bench=. -benchmem and compare versions with benchstat.
Fuzzing
FuzzXxx(f *testing.F) with seeds and property checks, run with go test -fuzz.
Code Coverage
go test -coverprofile=cover.out and go tool cover -html=cover.out.
Coverage is a map, not a score:
Lines can be covered without being checked. Use coverage to find untested code.
Summary
Benchmark before optimizing, fuzz code that handles untrusted input, and use coverage to find gaps.
Writing Benchmarks in Go
Measure Go code speed and allocations with testing.B, b.Loop and -benchmem, avoid benchmarks the compiler optimizes away, and compare results with benchstat.
Fuzz Testing in Go
Write Go fuzz tests with testing.F to find crashes and edge cases automatically, run them with -fuzz, and turn failures into permanent regression tests.
Code Coverage in Go
Measure which lines your Go tests run with -cover and -coverprofile, view an HTML coverage report, and use coverage numbers sensibly.