Skip to content
All posts

Table-Driven Tests for People Who Hate Writing Tests

One slice of cases, one loop, t.Run — and suddenly testing feels like filling in a spreadsheet.

Kampala Gophers Team3 min read

main.go
func TestFormatUGX(t *testing.T) {
	tests := []struct {
		name string
		in   int64
		want string
	}{
		{"zero", 0, "UGX 0"},
		{"thousands", 125000, "UGX 125,000"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := FormatUGX(tt.in); got != tt.want {
				t.Errorf("FormatUGX(%d) = %q, want %q", tt.in, got, tt.want)
			}
		})
	}
}

// Weekly Intel

Get the next post in your inbox

Event updates and the occasional Go deep-dive. No spam, unsubscribe any time.