Go testing and benchmarks: table tests, go test, pprof
13 steps · about 4 h 50 min · Intermediate
- Add a test - The Go Programming Language (tutorial, go.dev, 15 min)
Start with the official walkthrough of _test.go files, Test functions and running go test, including a failing test.
- How to Write Go Code - The Go Programming Language (guide, go.dev, 15 min)
Shows the test framework in a real package, with a first small table of cases and the go test output.
- Go Wiki: LearnTesting - The Go Programming Language (reference, go.dev wiki, 10 min)
The Go team's own map of testing resources, so you can see what exists before going deeper.
- Using Subtests and Sub-benchmarks - The Go Programming Language (blog, go.dev blog, 20 min)
The canonical explanation of table-driven tests, t.Run subtests and table-driven benchmarks, from the people who added them.
- Go Wiki: Go Test Comments - The Go Programming Language (guide, go.dev wiki, 10 min)
Once you can write tables, learn when to use t.Fatal or t.Error and when to split into separate test functions.
- Golang Table Driven Tests: The Idiomatic Way (tutorial, golangtutorial.com, 25 min)
Practise the pattern with parallel cases, error tables and the Go 1.22 loop-variable change.
- Go Course: Testing (tutorial, Karan Pratap Singh, DEV Community, 20 min)
Adds coverage with -coverprofile and go tool cover, plus a first look at fuzzing, to round out go test.
- How to Benchmark Go with the testing Package? - CodSpeed Docs (doc, CodSpeed, 20 min)
Moves from tests to testing.B, sub-benchmarks across input sizes, and linking benchmarks to pprof.
- Benchmarking & profiling (article, GopherTrunk, 15 min)
A short summary of the difference between how slow (benchmark) and where (profile), with -benchmem and -cpuprofile.
- Profiling Go Programs - The Go Programming Language (blog, go.dev blog, 40 min)
The official worked example of reading go tool pprof output and speeding up a program by an order of magnitude.
- High Performance Go Workshop (video, Dave Cheney, 1 h)
A deeper workshop on benchmarking pitfalls and profiling benchmarks with CPU, memory and block profiles.
- A Practical Guide to Profiling in Go - The JetBrains Blog (article, JetBrains, 25 min)
A recent tour of every profile type and how to collect them with runtime/pprof and net/http/pprof.
- Diagnostics - The Go Programming Language (doc, go.dev, 15 min)
Finish with the official overview of profiling, tracing and debugging so you know which tool to reach for.