Testing and debugging

go test ships with the language, so a test is a file beside the code that it tests, not a project of its own. These lessons cover test files and table tests, testify, httptest, fakes and mocks, golden files, fuzzing, benchmarks, the race detector and coverage, and the profile that names the hot function. The part ends on go vet and the linters around it.

4.1 go test, test files and the testing package a _test.go file, TestXxx, t.Errorf against t.Fatalf, go test -v and a failing test Open 4.2 Table tests, subtests and parallel tests a table of cases, t.Run, t.Parallel and a failure that names its case Open 4.3 testify and the libraries around it require and assert, a suite, and what the helpers add to the testing package Open 4.4 Testing HTTP handlers with httptest httptest.NewRecorder, a handler called directly, httptest.NewServer and a client against it Open 4.5 Fakes, stubs and interfaces an interface at the seam, a fake store, a stub that returns an error and a test that needs no network Open 4.6 Mocks: gomock and hand-written doubles a hand-written mock, gomock in words, expectations and why a fake is often enough Open 4.7 Golden files and testdata a golden file under testdata, an -update flag and a diff that shows what changed Open 4.8 Native fuzzing F.Fuzz, a seed corpus, go test -fuzz and the input that crashes the parser Open 4.9 Benchmarks and benchstat BenchmarkXxx, b.N, -benchmem, the allocs per operation and benchstat in words Open 4.10 The race detector and coverage go test -race, a data race reported with both stacks, -cover and a coverage profile Open 4.11 Profiling with pprof net/http/pprof, a CPU profile, go tool pprof and the hot function it names Open 4.12 go vet, staticcheck and golangci-lint go vet, the checks it runs, staticcheck and golangci-lint named with what this image does not have Open

Lessons