go 42 lines · 7 steps

Table-driven tests in Go

A guarded integer divide paired with a table-driven test that runs each case as its own subtest.

Explained by highlit
1package calc
2 
3import "testing"
4 
5func Divide(a, b int) (int, error) {
6 if b == 0 {
7 return 0, errDivByZero
8 }
9 return a / b, nil
10}
11 
12var errDivByZero = &divError{}
13 
14type divError struct{}
15 
16func (*divError) Error() string { return "division by zero" }
17 
18func TestDivide(t *testing.T) {
19 tests := []struct {
20 name string
21 a, b int
22 want int
23 wantErr bool
24 }{
25 {"simple", 10, 2, 5, false},
26 {"truncates", 7, 2, 3, false},
27 {"negative", -9, 3, -3, false},
28 {"by zero", 1, 0, 0, true},
29 }
30 
31 for _, tt := range tests {
32 t.Run(tt.name, func(t *testing.T) {
33 got, err := Divide(tt.a, tt.b)
34 if (err != nil) != tt.wantErr {
35 t.Fatalf("Divide(%d, %d) error = %v, wantErr %v", tt.a, tt.b, err, tt.wantErr)
36 }
37 if got != tt.want {
38 t.Errorf("Divide(%d, %d) = %d, want %d", tt.a, tt.b, got, tt.want)
39 }
40 })
41 }
42}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Returning a sentinel error value lets callers compare against a known failure rather than parsing strings.
  2. 2A slice of anonymous structs turns many test cases into one readable, easy-to-extend table.
  3. 3t.Run gives each case its own named subtest, so failures point straight at the offending input.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Table-driven tests in Go — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code