Golang & Concurrency (50 Qs)•Part 3: Go Microservices, HTTP, gRPC & Architecture (33 - 44)
Table-Driven Unit Testing and Subtests in Go (`t.Run`).
senior LevelCode ExampleTestingSystem DesigngRPC
In Simple Words: The idiomatic Go testing pattern uses slices of test cases:
go
func TestAdd(t *testing.T) {
tests := []struct {
name string
a, b int
expected int
}{
{"positive numbers", 2, 3, 5},
{"negative numbers", -1, -1, -2},
{"zero", 0, 5, 5},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := Add(tt.a, tt.b)
if result != tt.expected {
t.Errorf("got %d, want %d", result, tt.expected)
}
})
}
}