table-driven test
A Go test style that lists many input cases and expected results as rows in a slice, then checks them all in one loop.
In more detail
Each row is a small struct with a name, inputs and the wanted result. Adding a case becomes adding a row, which makes edge cases such as empty input easy to cover. Running each row with t.Run gives every failure its own name.
Example
cases := []struct {
name string
code int
want bool
}{
{"ok", 200, true},
}Related terms
- subtest — A named test run inside another test, with its own pass or fail result, often one per case in a table of inputs.
- test function — A Go function whose name starts with Test and which takes a *testing.T, so go test finds and runs it automatically.
- edge case — An input at the limits of what code must handle, such as an empty list, zero, a single item or the largest allowed value.
- struct — A Go type that groups named fields of different types into one value, such as a Target with a Name and a Port.
Taught in
- Writing Table Driven Tests — Concurrent Systems Programming with Go