typestar

Testing

13 steps in 5 sets of Go.

Testing is in the toolchain, not a library you choose. Writing tests, table-driven cases, fakes and fixtures, benchmarks and fuzzing, and the flags that control what runs.

Thirteen steps. The conventions are strict and nobody argues about them, which is a fair summary of Go generally.

Start this tour

Writing tests

  • A Go testA file ending _test.go, a function taking *testing.T, and t.Errorf.
  • Table-driven testsThe Go house style: a slice of cases, one subtest each.
  • Helpers and cleanupt.Helper fixes the reported line; t.Cleanup replaces teardown.
  • Testing errorsAssert on the sentinel or the type, never on the message text.

Fakes & fixtures

Measuring & generating

  • Benchmarksb.Loop in Go 1.24 replaced the b.N loop, and stops the optimizer cheating.
  • FuzzingA fuzz target gets random input until it finds one that breaks the property.
  • Examples are testsAn Example function with an Output comment is compiled, run and checked.

Running them

  • Parallel subtestst.Parallel pauses a subtest until the others have started, then runs them together.
  • TestMainOne place for package-level setup, and the exit code is yours to return.

Encore

  • store_test.goA whole test file: table tests, subtests, a fake, a benchmark and a fuzz target.

The other Go tours