Table-Driven Tests

Table-driven tests allow you to test multiple input-output scenarios with a single test function.

tests := []struct {
  input int
  want  int
}{
  {2, 4}, {3, 6}, {4, 8},
}

for _, tt := range tests {
  got := Double(tt.input)
  if got != tt.want {
    t.Errorf("Double(%d) = %d; want %d", tt.input, got, tt.want)
  }
}
← PrevNext →