Go verifies programs with the standard testing package (unit tests, table-driven tests, benchmarks, fuzzing, examples) run by go test, and validates data with explicit checks or a library such as go-playground/validator. A reliable setup combines fast unit tests, integration tests for real dependencies, the race detector, coverage reporting and input validation at the system boundary.
This guide covers the techniques in the order you normally adopt them, with runnable code.
Put tests in a file ending _test.go, name each function TestXxx, and take a *testing.T.
Use t.Errorf to record a failure and keep going, and t.Fatalf when later steps make no sense after the failure. Use t.Helper() in helper functions so failures point to the caller.
List the cases in a slice and loop. Each case becomes a named subtest that you can run alone with go test -run 'TestDivide/zero'.
Compare errors with errors.Is or errors.As, not string matching. For struct and slice results, cmp.Diff from github.com/google/go-cmp/cmp prints a readable difference.
Fuzzing (built in since Go 1.18) generates inputs to find crashes and broken invariants.
Run it with go test -fuzz=FuzzReverse -fuzztime=30s. Benchmarks use testing.B and a loop for b.Loop() (Go 1.24 and later) or for i := 0; i < b.N; i++, run with go test -bench . -benchmem. Example functions (ExampleDivide) with an // Output: comment are compiled, run and shown in generated documentation.
go test -race ./... in CI to find data races.httptest.NewRecorder and clients with httptest.NewServer.t.TempDir(), t.Setenv() and t.Cleanup() so tests do not leak state.testing/synctest to test code that depends on time or goroutines without real sleeps.testing.Short() or build tags and run them in a different CI job.Coverage helps find untested branches, not prove correctness:
Testing proves your code works; validation protects it from bad input at runtime. Validate at the boundary (HTTP requests, config files, messages) and return clear errors.
errors.Join (Go 1.20) returns all problems at once. For tag-based rules, github.com/go-playground/validator/v10 reads tags such as validate:"required,email", but keep tests for the rules you rely on. Always parse input into a typed value instead of passing raw strings around.
It is a test that loops over a slice of input and expected-output cases and runs each as a subtest. It is the standard Go style because adding a case is one line.
Use go test -run TestName with a regular expression, and add -v for verbose output. Subtests are selected with a slash, such as -run 'TestDivideTable/zero'.
No. The standard library is enough. Testify adds assertion helpers and mocks that some teams like, but it is optional.
Inject a clock or function for the current time, or use testing/synctest in Go 1.25 and later so timers run on a fake clock.
Write table-driven unit tests with the standard library, add fuzzing for parsers and invariants, run the race detector and coverage in CI, and keep integration tests separate. Validate external input explicitly at the boundary, and return all errors together with errors.Join when it helps callers.