go-testing
Original:🇺🇸 English
Translated
This skill should be used when the user asks to "write Go unit tests", "add tests to a Go package", "use the testing package", "write table-driven tests in Go", or needs guidance on Go test patterns, subtests, benchmarks, and test helpers.
2installs
Added on
NPX Install
npx skill4agent add the-perfect-developer/the-perfect-opencode go-testingTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Go Testing
The package provides support for automated testing of Go packages. Tests are run with and require no external libraries — the standard library covers unit tests, benchmarks, fuzz tests, and example functions.
testinggo testFile and Function Conventions
Test files must end in . They are excluded from normal builds but included by .
_test.gogo testTest functions must match the signature where does not start with a lowercase letter:
func TestXxx(*testing.T)Xxxgo
package mypackage
import "testing"
func TestAdd(t *testing.T) {
got := Add(2, 3)
if got != 5 {
t.Errorf("Add(2, 3) = %d; want 5", got)
}
}White-box vs Black-box Tests
- Same package () — accesses unexported identifiers
package mypackage - suffix package (
_test) — tests only the exported API; this is "black-box" testingpackage mypackage_test
Both styles can coexist in the same directory.
Reporting Failures
| Method | Behaviour |
|---|---|
| Marks failed, continues execution |
| Marks failed, continues execution |
| Marks failed, stops test immediately |
| Marks failed, stops test immediately |
| Marks failed without logging, continues |
| Marks failed without logging, stops immediately |
Use / when checking multiple independent conditions so all failures are reported. Use / when a failure makes further checks meaningless (e.g., a nil pointer).
ErrorfErrorFatalfFatalPrefer over unless subsequent assertions depend on a prior one succeeding.
t.Errorft.FatalfTable-Driven Tests
Table-driven tests are the idiomatic Go pattern for testing a function against many inputs:
go
func TestDivide(t *testing.T) {
tests := []struct {
name string
a, b float64
want float64
wantErr bool
}{
{name: "positive", a: 10, b: 2, want: 5},
{name: "negative divisor", a: 10, b: -2, want: -5},
{name: "divide by zero", a: 10, b: 0, wantErr: true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got, err := Divide(tc.a, tc.b)
if (err != nil) != tc.wantErr {
t.Fatalf("Divide(%v, %v) error = %v, wantErr %v", tc.a, tc.b, err, tc.wantErr)
}
if !tc.wantErr && got != tc.want {
t.Errorf("Divide(%v, %v) = %v; want %v", tc.a, tc.b, got, tc.want)
}
})
}
}Name each case descriptively. Use so each case appears as a named subtest in output and can be run individually.
t.RunSubtests
t.Run(name, func(t *testing.T))- Appear in output as
TestParent/SubName - Can be run individually:
go test -run TestParent/SubName - Share setup/teardown with the parent
- Can be run in parallel independently of other top-level tests
go
func TestAPI(t *testing.T) {
server := startTestServer(t) // shared setup
t.Run("GET /users", func(t *testing.T) {
// ...
})
t.Run("POST /users", func(t *testing.T) {
// ...
})
// server is cleaned up after all subtests finish
}Parallel Tests
Call at the start of a test function to allow it to run concurrently with other parallel tests:
t.Parallel()go
func TestExpensive(t *testing.T) {
t.Parallel()
// ...
}For parallel subtests in a table-driven test, capture the loop variable:
go
for _, tc := range tests {
tc := tc // capture range variable (required before Go 1.22)
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
// use tc safely
})
}From Go 1.22 onward, loop variable capture is automatic and the line is no longer needed.
tc := tcCleanup
t.Cleanup(f func())go
func TestWithDB(t *testing.T) {
db := openTestDB(t)
t.Cleanup(func() { db.Close() })
// test body — db.Close is called automatically when test ends
}Prefer over inside test helpers because it runs after all subtests complete, not just when the helper function returns.
t.CleanupdeferTest Helpers
Mark a function as a test helper with so error output points to the call site, not inside the helper:
t.Helper()go
func assertEqualInts(t *testing.T, got, want int) {
t.Helper() // makes error line point to the caller
if got != want {
t.Errorf("got %d; want %d", got, want)
}
}Always call as the first statement in helper functions.
t.Helper()Temporary Directories
t.TempDir()go
func TestWriteFile(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "output.txt")
// write to path — dir is cleaned up automatically
}Environment Variables
t.Setenv(key, value)go
func TestWithEnv(t *testing.T) {
t.Setenv("MY_CONFIG", "test-value")
// original value restored after test
}Skipping Tests
Skip a test conditionally using , , or :
t.Skipt.Skipft.SkipNowgo
func TestIntegration(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
// ...
}
func TestRequiresDocker(t *testing.T) {
if os.Getenv("DOCKER_HOST") == "" {
t.Skip("DOCKER_HOST not set")
}
// ...
}Example Functions
Example functions serve as documentation and are verified by :
go testgo
func ExampleAdd() {
fmt.Println(Add(1, 2))
// Output: 3
}The comment is compared against stdout. Examples without an output comment are compiled but not executed. Use when output order is non-deterministic.
// Output:// Unordered output:Naming conventions:
go
func Example() { ... } // package example
func ExampleAdd() { ... } // function Add
func ExampleCalc() { ... } // type Calc
func ExampleCalc_Add() { ... } // method Calc.Add
func ExampleAdd_second() { ... } // second example for Add (suffix starts lowercase)TestMain
TestMain_test.gogo
func TestMain(m *testing.M) {
// setup
code := m.Run()
// teardown
os.Exit(code)
}Use for package-level resources (database connections, server processes). It is not necessary for per-test resources — use instead.
TestMaint.CleanupRunning Tests
bash
# Run all tests in the current module
go test ./...
# Run with verbose output
go test -v ./...
# Run tests matching a pattern (regexp)
go test -run TestAdd ./...
# Run a specific subtest
go test -run TestDivide/divide_by_zero ./...
# Run with race detector
go test -race ./...
# Run with coverage
go test -cover ./...
# Generate HTML coverage report
go test -coverprofile=coverage.out ./... && go tool cover -html=coverage.out
# Skip slow tests
go test -short ./...
# Set test timeout
go test -timeout 30s ./...
# Run benchmarks
go test -bench=. ./...
# Run benchmarks with memory allocation stats
go test -bench=. -benchmem ./...Quick Reference: testing.T
Methods
testing.T| Method | Purpose |
|---|---|
| Create named subtest |
| Mark test as parallel |
| Mark as helper function |
| Register teardown function |
| Create auto-cleaned temp directory |
| Set env var, auto-restored after test |
| Change working dir, auto-restored |
| Context canceled before cleanup runs |
| Log (shown on failure or with |
| Log formatted |
| Fail + log, continue |
| Fail + log formatted, continue |
| Fail + log, stop |
| Fail + log formatted, stop |
| Skip + log, stop |
| Skip + log formatted, stop |
| Return full test name |
| Reports whether test has failed |
| Returns test deadline from |
Additional Resources
For benchmarks, fuzz testing, and advanced patterns:
- —
references/benchmarks-and-fuzzing.mdAPI,testing.Bstyle, parallel benchmarks,b.Loop()fuzz tests,testing.Fpatterns, andTestMainAllocsPerRun