By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
The first test checked one call: add(2, 3). Real functions have many cases: positive numbers, negative numbers, zero, an empty list, an error path. Copying the whole test function for each case produces a wall of near-duplicate code, and the interesting part, the case itself, disappears in the middle of it.
A table test puts the cases in a slice and runs the same checks for every row. The check is written once; the cases read like a checklist a reviewer can scan in a few seconds.
Here is the divide function from step 18 with a table test. Each row is an anonymous struct literal: a struct with no name, declared right where the slice is created, because no other code needs that type.
// ==== file: divide.go ====
package main
import (
"errors"
"fmt"
)
func divide(a, b int) (int, error) {
if b == 0 {
return 0, errors.New("cannot divide by zero")
}
return a / b, nil
}
func main() {
result, err := divide(10, 2)
fmt.Println(result, err)
}
// ==== file: divide_test.go ====
package main
import "testing"
func TestDivide(t *testing.T) {
tests := []struct {
name string
a int
b int
want int
wantErr bool
}{
{name: "exact division", a: 10, b: 2, want: 5},
{name: "integer division truncates", a: 7, b: 2, want: 3},
{name: "zero dividend", a: 0, b: 5, want: 0},
{name: "divide by zero", a: 1, b: 0, wantErr: true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got, err := divide(tc.a, tc.b)
if tc.wantErr {
if err == nil {
t.Fatalf("divide(%d, %d) returned no error", tc.a, tc.b)
}
return
}
if err != nil {
t.Fatalf("divide(%d, %d) returned error: %v", tc.a, tc.b, err)
}
if got != tc.want {
t.Errorf("divide(%d, %d) = %d; want %d", tc.a, tc.b, got, tc.want)
}
})
}
}Read the table in three pieces: the slice type, one line per case, and the loop that runs the checks. When you add a case later, you add one line and nothing else changes.
| Field | Used by the test for |
|---|---|
name | The subtest name, so a failure says which case broke. |
a, b | The inputs passed to divide. |
want | The expected result when the call succeeds. |
wantErr | Whether this case should produce an error instead of a value. |
t.Run(name, func(t *testing.T) { ... }) runs a block as a named subtest. The name appears in the output, so a failure points at TestDivide/divide_by_zero instead of a bare line number. The block is a closure, and it captures the current row tc.
Subtest names cannot contain spaces in the output. Go replaces them with underscores, so divide by zero appears as divide_by_zero, as the -v run below shows.
$ go test -v ./...
=== RUN TestDivide
=== RUN TestDivide/exact_division
=== RUN TestDivide/integer_division_truncates
=== RUN TestDivide/zero_dividend
=== RUN TestDivide/divide_by_zero
--- PASS: TestDivide (0.00s)
--- PASS: TestDivide/exact_division (0.00s)
--- PASS: TestDivide/integer_division_truncates (0.00s)
--- PASS: TestDivide/zero_dividend (0.00s)
--- PASS: TestDivide/divide_by_zero (0.00s)
PASS
ok penny 0.003sWhen several tables need the same check, move the check into a helper function and call t.Helper() on its first line. Go then reports the failure at the line that called the helper, not inside the helper, so the location still points at your case.
Edge cases matter more than happy cases: an empty slice, a zero amount, a negative refund. Here is a table for Penny's expense total:
// ==== file: penny.go ====
package main
import "fmt"
// Expense is one row in Penny's list. Money is stored in integer cents.
type Expense struct {
ID int `json:"id"`
Description string `json:"description"`
Cents int `json:"cents"`
Category string `json:"category"`
}
func totalCents(expenses []Expense) int {
total := 0
for _, expense := range expenses {
total += expense.Cents
}
return total
}
func main() {
expenses := []Expense{{ID: 1, Cents: 450}, {ID: 2, Cents: 1250}}
fmt.Println(totalCents(expenses))
}
// ==== file: penny_test.go ====
package main
import "testing"
func checkTotal(t *testing.T, expenses []Expense, want int) {
t.Helper()
if got := totalCents(expenses); got != want {
t.Errorf("got %d; want %d", got, want)
}
}
func TestTotalCents(t *testing.T) {
tests := []struct {
name string
expenses []Expense
want int
}{
{name: "empty list", expenses: nil, want: 0},
{name: "one expense", expenses: []Expense{{ID: 1, Cents: 450}}, want: 450},
{name: "zero cents", expenses: []Expense{{ID: 1, Cents: 0}}, want: 0},
{name: "refund is negative", expenses: []Expense{{ID: 1, Cents: 1000}, {ID: 2, Cents: -250}}, want: 750},
{name: "several expenses", expenses: []Expense{{ID: 1, Cents: 100}, {ID: 2, Cents: 200}, {ID: 3, Cents: 300}}, want: 600},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
checkTotal(t, tc.expenses, tc.want)
})
}
}Those five rows are a short specification of totalCents: an empty list totals zero, one expense adds up, zero contributes nothing, a refund reduces the total, and many expenses are summed. A reader learns the rule from the table.
When one row fails, the output names the subtest and prints your got/want message. Change the refund row's want to 1000 on purpose to see it:
$ go test ./...
--- FAIL: TestTotalCents (0.00s)
--- FAIL: TestTotalCents/refund_is_negative (0.00s)
penny_test.go:27: got 750; want 1000
FAIL
exit status 1
FAIL penny 0.004sAdd two rows to the TestDivide table: divide(-10, 2) and divide(10, -2). In Go, integer division truncates toward zero, so predict both answers before you run. Add a divide(-10, -2) row too. Then run go test -v ./... and confirm that each subtest name appears, and that all three new cases pass.
Key takeaways
A table test keeps cases in a slice of anonymous structs and runs one shared check for each row.
t.Run names each case, so failures point at one subtest instead of an anonymous line.
t.Helper makes failure locations point at the caller, which keeps a shared check useful.
Good tables include edge cases: empty, zero and negative, not only ordinary values.
A fail message should include got and want so the failure explains itself.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.