100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Go Testing Cheat Sheet

Go Testing Cheat Sheet

Covers writing unit tests and table-driven tests with the testing package, subtests, benchmarks, and running tests with the go test CLI.

2 PagesIntermediateApr 5, 2026

Basic Test

The minimum shape of a Go unit test.

go
// math.gofunc Add(a, b int) int { return a + b }// math_test.gopackage mathpkgimport "testing"func TestAdd(t *testing.T) {    got := Add(2, 3)    want := 5    if got != want {        t.Errorf("Add(2, 3) = %d; want %d", got, want)   // Reports failure, continues test    }}

Table-Driven Tests & Subtests

The idiomatic way to cover many cases with one test body.

go
func TestAddTable(t *testing.T) {    cases := []struct {        name     string        a, b     int        expected int    }{        {"positive", 2, 3, 5},        {"negative", -1, -1, -2},        {"zero", 0, 0, 0},    }    for _, tc := range cases {        t.Run(tc.name, func(t *testing.T) {   // Named subtest, runs independently            got := Add(tc.a, tc.b)            if got != tc.expected {                t.Fatalf("got %d, want %d", got, tc.expected)  // Stops this subtest immediately            }        })    }}

Benchmarks & Running Tests

Measuring performance and driving tests from the CLI.

go
func BenchmarkAdd(b *testing.B) {    for i := 0; i < b.N; i++ {        Add(2, 3)    }}// go test ./...                # Run all tests in the module// go test -v ./...             # Verbose output// go test -run TestAdd         # Run tests matching a regex// go test -cover ./...         # Report code coverage// go test -bench=.             # Run benchmarks

Concepts

Conventions the testing package relies on.

  • _test.go suffix- Test files must end in _test.go and live in the same package (or pkgname_test)
  • testing.T- t.Error/t.Errorf mark failure and continue; t.Fatal/t.Fatalf mark failure and stop the test
  • Table-driven tests- Idiomatic Go pattern: a slice of input/expected cases run through one shared test body
  • t.Run- Creates named subtests, each reported individually and runnable in isolation with -run
  • testing.B- Used for benchmarks; b.N is set by the framework to get stable timing
  • go test -cover- Reports statement coverage; -coverprofile=c.out produces a detailed report
  • Mocks/stubs- Go favors small interfaces plus hand-written fakes over heavy mocking frameworks
Pro Tip

Use table-driven tests with t.Run subtests by default — it keeps cases readable, lets you re-run a single failing case with go test -run TestName/case_name, and scales cleanly as cases grow.

Was this cheat sheet helpful?

Explore Topics

#GoTesting#GoTestingCheatSheet#Programming#Intermediate#BasicTest#Table#Driven#Tests#Testing#CommandLine#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet