Testing in Go

From the Go cheat sheet · Testing · verified Jul 2026

Testing

Unit tests and benchmarks

go
// math_test.go
func TestAdd(t *testing.T) {
    result := Add(2, 3)
    if result != 5 {
        t.Errorf("Add(2, 3) = %d; want 5", result)
    }
}

// Run tests
// go test
// go test -v
// go test -cover
🧪 Built-in testing with testing package
📊 Table-driven tests for multiple cases
⚡ Benchmarks with go test -bench
📈 Coverage with go test -cover
Back to the full Go cheat sheet