Back to course

Table-Driven Tests: The Go Standard

Go (Golang) for Cloud-Native Microservices

Table-Driven Testing

This is the idiomatic way to write tests in Go. You define a slice of structs containing inputs and expected outputs.

go tests := []struct { name string input int expected int }{ {"positive", 1, 2}, {"negative", -1, 0}, }

for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // run test }) }