Stage 3 · Build
Go Fundamentals for SREs
Testing & Benchmarking
Table-driven tests, testify, benchmarks, and fuzzing for reliable infrastructure code.
Test Basics
Go has a built-in test framework. Tests live in _test.go files, use the testing package, and run with go test. No external test runner or framework is required.
Table-Driven Tests
Table-driven tests are the idiomatic Go pattern. Define a slice of test cases, each with inputs and expected outputs, and loop through them.
Subtests and Parallel Execution
Subtests let you organize related assertions. Adding t.Parallel() runs subtests concurrently, which speeds up test suites significantly.
Testify Assertions
The testify package provides assertion and suite utilities. Assertions reduce boilerplate and produce clearer failure messages.
Benchmarks
Go's testing package includes benchmarking. Benchmark functions take a *testing.B and use a loop with b.N iterations. The test framework adjusts b.N to get stable measurements.
Fuzzing
Go 1.18+ has built-in fuzzing. Fuzz tests generate random inputs to find edge cases your test cases missed. This is invaluable for parsers, validators, and deserializers.
Fuzz tests are slow and consume resources. Run them in CI with a time limit: go test -fuzz=FuzzParseDuration -fuzztime=30s. The fuzzer saves interesting inputs to testdata/fuzz/ for regression testing.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.