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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Returning a sentinel error value lets callers compare against a known failure rather than parsing strings.
- 2A slice of anonymous structs turns many test cases into one readable, easy-to-extend table.
- 3t.Run gives each case its own named subtest, so failures point straight at the offending input.
Related explainers
go
package main import ( "errors"
Parsing and validating CLI flags in Go
cli-parsing
validation
error-handling
Intermediate
8 steps
java
public class ThumbnailProcessor { private static final int MAX_CONCURRENCY = 4;
Bounded parallel thumbnail rendering in Java
concurrency
thread-pool
futures
Intermediate
7 steps
go
package cache import ( "container/list"
Building a generic LRU cache in Go
lru-cache
generics
linked-list
Intermediate
8 steps
go
package model import ( "encoding/json"
Custom JSON marshaling in Go
json
serialization
interfaces
Intermediate
5 steps
go
func (h *TransactionHandler) ExportCSV(c *gin.Context) { ctx := c.Request.Context() filters := parseTransactionFilters(c)
Streaming a CSV export in Gin
streaming
csv-export
database-cursor
Intermediate
8 steps
php
<?php namespace App\Rules;
How a custom phone validation rule works in Laravel
validation
custom-rules
dependency
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/table-driven-tests-in-go-explained-go-b61a/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.