Why Go's testing story is different
In most ecosystems, setting up tests involves choosing a framework, installing dependencies, and wiring things together. Go ships with everything built in. The testing package is part of the standard library, and go test is part of the toolchain. You can write meaningful tests from day one without any external dependencies.
Because the tools come with the language, Go projects tend to test the same way, and you will meet the testing package in every Go codebase you work on.
Test files and the go test command
Go's build system uses file naming to separate test code from production code. Any file whose name ends with _test.go is a test file. These files are compiled only when running tests and are never included in your production binary.
The convention is to keep test files alongside the code they test, in the same directory. If you have math.go, the tests live in math_test.go.
To run tests, use go test:
go test ./... # run all tests in the current modulego test ./pkg/math # run tests in one packagego test -v ./... # verbose: show test names and t.Log outputgo test -run TestSum # only tests matching the patternThe -v flag is especially useful during development: it prints t.Log output even for passing tests, which helps trace what a test was doing before it failed.
Writing your first test
A test function must follow two rules: its name starts with Test followed by a name beginning with a capital letter, and it accepts exactly one argument, a pointer to testing.T.
// math.gopackage mathfunc Sum(a, b int) int { return a + b}// math_test.gopackage mathimport "testing"func TestSum(t *testing.T) { got := Sum(2, 3) want := 5 if got != want { t.Errorf("Sum(2, 3) = %d, want %d", got, want) }}Running go test in this directory either prints an ok line or a failure with the message you provided. The test name in the report comes directly from the function name.
The *testing.T type
*testing.T is your handle on the test runner. It lets you report failures, emit log messages, create subtests, and register cleanup functions. Here are the most commonly used methods:
| Method | Behavior |
|---|---|
Log(args...) | Prints a message; only visible with -v or on failure |
Logf(format, args...) | Formats and prints a message |
Error(args...) | Marks the test failed; execution continues |
Errorf(format, args...) | Formats the message, marks failed, continues |
Fatal(args...) | Marks the test failed; stops execution immediately |
Fatalf(format, args...) | Formats the message, marks failed, stops |
Skip(args...) | Marks the test skipped and stops execution |
Helper() | Marks the calling function as a helper (adjusts error line numbers) |
Run(name, f) | Runs f as a named subtest |
Cleanup(f) | Registers f to run after the test or subtest completes |
Parallel() | Allows this test to run in parallel with other parallel tests |
When you write a helper function that calls t.Error or t.Fatal, mark it with t.Helper(). The failure in the output then points to the line that called the helper, which is usually the line you need to look at, instead of a line inside the helper.
Error vs Fatal
Error and Errorf mark the test as failed but allow the rest of the function to continue running. Use them when you want to check multiple independent conditions and report all failures at once:
func TestConfig(t *testing.T) { cfg := LoadConfig("testdata/config.json") if cfg.Host == "" { t.Error("Host should not be empty") } if cfg.Port == 0 { t.Error("Port should not be zero") }}If both fields are wrong, a single test run reports both problems.
Fatal and Fatalf mark the test as failed and stop execution of the current test function immediately. Use them when a subsequent step would either panic or produce meaningless results without the prior step succeeding:
func TestUserLoad(t *testing.T) { user, err := LoadUser(42) if err != nil { t.Fatalf("LoadUser(42) failed: %v", err) } // safe to reach here — Fatal would have stopped us if err != nil if user.Name == "" { t.Error("Name should not be empty") }}If LoadUser returns an error and you used t.Error instead of t.Fatal, the next line would likely panic on a nil pointer. t.Fatal prevents that.
Table-driven tests
The dominant testing pattern in Go is the table-driven test. Instead of writing separate functions for each scenario, you define a slice of test cases and loop over them:
func TestSum(t *testing.T) { tests := []struct { name string a, b int want int }{ {"positive numbers", 2, 3, 5}, {"negative numbers", -1, -2, -3}, {"zero", 0, 0, 0}, {"mixed signs", -1, 1, 0}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := Sum(tt.a, tt.b) if got != tt.want { t.Errorf("Sum(%d, %d) = %d, want %d", tt.a, tt.b, got, tt.want) } }) }}Adding a new test case is a one-line change in the slice. Each case runs as a named subtest via t.Run, so failure output like --- FAIL: TestSum/mixed_signs tells you exactly which case broke. You can also run a single case in isolation:
go test -run TestSum/mixed_signsThe same shape works for a two-line function and for behavior with dozens of edge cases. You will find it all over the Go standard library and in most well-maintained Go projects.
Subtests and parallel execution
t.Run(name, f) runs f as a subtest of the current test. The parent test waits for all subtests to finish before completing. Subtests can themselves call t.Run, giving you nested test hierarchies when needed.
Subtests can be run in parallel by calling t.Parallel() at the start of the subtest function:
for _, tt := range tests { tt := tt // capture the loop variable t.Run(tt.name, func(t *testing.T) { t.Parallel() got := Sum(tt.a, tt.b) if got != tt.want { t.Errorf("Sum(%d, %d) = %d, want %d", tt.a, tt.b, got, tt.want) } })}Cleanup
t.Cleanup(f) registers a function to be called when the test (or subtest) finishes, regardless of whether it passes or fails. It is the test-aware equivalent of defer:
func TestWithFile(t *testing.T) { f, err := os.CreateTemp("", "test-*.txt") if err != nil { t.Fatal(err) } t.Cleanup(func() { os.Remove(f.Name()) }) // write to f, run assertions ...}For temporary directories specifically, t.TempDir() is a convenience method that creates a directory and registers its deletion for you:
dir := t.TempDir()// dir is removed after the test completesThe advantage of t.Cleanup over a bare defer is that cleanup functions registered in a parent test run after all subtests finish, which defer cannot guarantee.
TestMain
Sometimes you need setup that runs once before any test in a package, such as opening a database connection, populating shared fixtures or starting a background server. TestMain is where that goes.
package math_testimport ( "os" "testing")func TestMain(m *testing.M) { // package-level setup setup() code := m.Run() // run all Test* functions in this package // package-level teardown teardown() os.Exit(code)}When TestMain is present, it replaces the default test runner as the entry point for the package's tests. m.Run() executes all TestXxx functions and returns an integer exit code. The exit code is how the test binary tells whoever ran it whether the tests passed, and the example passes it to os.Exit explicitly.
The testdata directory
When tests depend on external files (JSON fixtures, SQL migration scripts, expected output), place them in a testdata/ directory alongside the test files. Go's build tools ignore this directory, and the test runner sets the working directory to the package directory, so you can use relative paths directly:
data, err := os.ReadFile("testdata/input.json")if err != nil { t.Fatal(err)}The name testdata is a Go convention: the compiler and go build ignore any directory with that name.
Black-box testing
By default, a test file in package math can access unexported identifiers. This is white-box testing: the tests know the internals.
If you want to test only the exported API (black-box testing), name the test package math_test:
// math_test.gopackage math_testimport ( "math" "testing")func TestSum(t *testing.T) { // only exported identifiers are accessible here got := math.Sum(2, 3) if got != 5 { t.Errorf("got %d, want 5", got) }}You can have both package math and package math_test files in the same directory. The common approach is to use package math for tests that need access to unexported internals, and package math_test for tests that verify the public contract. Keeping them apart shows you when the public API accidentally depends on internal details.