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

Testing in Go

Write and run automated tests in Go using the built-in testing package, table-driven tests, and benchmarks.

Packages & ToolingIntermediate10 min readJul 8, 2026
Analogies

Introduction

Go ships with a built-in testing package and a matching go test command, so writing automated tests requires no third-party framework. Tests live alongside the code they cover, in files named with a _test.go suffix, and test functions follow the naming convention TestXxx(t *testing.T). This tight integration encourages a culture where testing is a routine part of development rather than an afterthought.

🏏

Cricket analogy: A domestic academy that has its own in-house nets and coaching staff built in, rather than needing to hire an outside academy, mirrors Go shipping a built-in testing package; files named _test.go and functions like TestYorker mirror a standard naming convention every coach recognizes.

Syntax

go
// file: mathutil/add_test.go
package mathutil

import "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)
    }
}

Explanation

A test function receives a *testing.T value used to report failures: t.Errorf logs a failure and continues the test, while t.Fatalf logs a failure and stops the current test immediately. Running go test in a package directory compiles and executes every TestXxx function it finds. For testing many input/output combinations without duplicating code, Go favors table-driven tests: a slice of struct literals describing cases, iterated with a loop that calls the function under test and checks each result, often using t.Run to create named subtests. The testing package also supports benchmarks, written as func BenchmarkXxx(b *testing.B) and run with go test -bench=., which repeatedly execute code to measure performance. Coverage can be measured with go test -cover, which reports the percentage of statements exercised by the test suite.

🏏

Cricket analogy: A scorer noting a no-ball but continuing the over is like t.Errorf logging a failure and continuing, while calling off the match for bad light is like t.Fatalf stopping immediately; running every net drill in one afternoon is go test running every TestXxx, checking many stances via t.Run is table-driven testing, timing bowling speed repeatedly is a benchmark, and checking pitch coverage used is like go test -cover.

Example

go
package mathutil

import "testing"

func TestAddTableDriven(t *testing.T) {
    cases := []struct {
        name     string
        a, b     int
        expected int
    }{
        {"positives", 2, 3, 5},
        {"with negative", -1, 5, 4},
        {"zeros", 0, 0, 0},
    }

    for _, tc := range cases {
        t.Run(tc.name, func(t *testing.T) {
            got := Add(tc.a, tc.b)
            if got != tc.expected {
                t.Errorf("Add(%d, %d) = %d; want %d", tc.a, tc.b, got, tc.expected)
            }
        })
    }
}

Output

bash
$ go test -v -cover ./...
=== RUN   TestAddTableDriven
=== RUN   TestAddTableDriven/positives
=== RUN   TestAddTableDriven/with_negative
=== RUN   TestAddTableDriven/zeros
--- PASS: TestAddTableDriven (0.00s)
    --- PASS: TestAddTableDriven/positives (0.00s)
    --- PASS: TestAddTableDriven/with_negative (0.00s)
    --- PASS: TestAddTableDriven/zeros (0.00s)
PASS
coverage: 100.0% of statements
ok      example.com/greeter/mathutil   0.002s

Key Takeaways

  • Test files end in _test.go and live in the same package/directory as the code under test.
  • Test functions follow func TestXxx(t *testing.T); run them with go test.
  • t.Errorf marks a failure and continues; t.Fatalf marks a failure and stops the test immediately.
  • Table-driven tests iterate a slice of input/expected-output cases, often with t.Run for subtests.
  • Benchmarks use func BenchmarkXxx(b *testing.B) and run via go test -bench=.; go test -cover reports coverage.

Practice what you learned

Was this page helpful?

Topics covered

#Go#GoProgrammingStudyNotes#Programming#TestingInGo#Testing#Syntax#Explanation#Example#StudyNotes#SkillVeris