By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Go has no test framework to install. A test is a function in a file ending in _test.go whose name starts with Test, takes *testing.T and lives in the same package as the code it tests (or in a _test package beside it). go test ./... compiles everything, runs every test and reports coverage. Because tests are compiled with the package, they catch signature drift immediately — a refactor that breaks a test fails the build, not a YAML configuration.
The default shape is the table test: a slice of named cases, one t.Run subtest per case, and a failure message that prints the inputs. It scales from three cases to three hundred without repeating the setup. t.Helper() keeps assertion helpers out of the failure line numbers, and t.Cleanup gives each subtest its own teardown.
Table tests prove behavior, benchmarks measure cost, fuzzing hunts panics and invariant breaks over generated input, and the race detector finds unsynchronized memory access. They answer different questions, so a serious package uses all four at a proportion that matches its risk: many unit tests, fewer integration tests, a handful of end-to-end checks, plus the race detector on every CI run.
package analyze_test
import (
"testing"
"relay/internal/analyze"
)
func TestCountEvents(t *testing.T) {
tests := []struct {
name string
events []analyze.Event
want int
}{
{
name: "empty batch",
events: nil,
want: 0,
},
{
name: "single event",
events: []analyze.Event{{Service: "api", Level: "info"}},
want: 1,
},
{
name: "mixed levels",
events: []analyze.Event{
{Service: "api", Level: "error"},
{Service: "worker", Level: "warn"},
{Service: "worker", Level: "info"},
},
want: 3,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := analyze.CountEvents(tt.events); got != tt.want {
t.Fatalf("CountEvents() = %d, want %d", got, tt.want)
}
})
}
}
func BenchmarkCountEvents(b *testing.B) {
events := make([]analyze.Event, 1000)
for i := range events {
events[i] = analyze.Event{Service: "api", Level: "info", Status: 200}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
analyze.CountEvents(events)
}
}
func FuzzParseLine(f *testing.F) {
f.Add([]byte(`{"service":"api","level":"info","status":200}`))
f.Add([]byte(`{}`))
f.Fuzz(func(t *testing.T, line []byte) {
event, err := analyze.ParseLine(line)
if err != nil {
return
}
if event.Service == "" {
t.Fatalf("ParseLine accepted an event without a service: %q", line)
}
})
}# Unit tests with verbose subtests.
go test ./internal/analyze -run TestCountEvents -v
# Every race in every package; the flag rebuilds with instrumentation.
go test -race ./...
# Compare allocations as well as time.
go test ./internal/analyze -bench BenchmarkCountEvents -benchmem -count=5
# Fuzz for a fixed budget; a crash is written to testdata/fuzz/.
go test ./internal/analyze -fuzz FuzzParseLine -fuzztime 10s
# Coverage profile you can open in a browser.
go test ./... -coverprofile=coverage.out
go tool cover -html=coverage.out| Tool | Question it answers | Command |
|---|---|---|
| Table test | Does the behavior match the spec? | go test -run TestName -v |
| Benchmark | How expensive is this path? | go test -bench . -benchmem |
| Fuzz target | Can any input break the invariant? | go test -fuzz FuzzName |
| Race detector | Is shared memory unsynchronized? | go test -race ./... |
| Coverage | Which statements did the tests execute? | go test -coverprofile=... |
Key takeaways
Tests are ordinary Go files compiled with the package, run by go test.
Table tests with t.Run subtests scale from a few cases to many and name failures clearly.
Benchmarks use b.N; fuzzing checks invariants over generated input.
go test -race must be a CI gate for any code that starts goroutines.
Tests prove behavior and wiring; logs, metrics and traces are how you understand production.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.