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

Taught in

References