Table-driven tests in Go

Go

The idiomatic Go pattern: a slice of cases and a subtest per entry, each named so failures are readable.

func TestSlugify(t *testing.T) {
    cases := []struct{ name, in, want string }{
        {"spaces", "Hello World", "hello-world"},
        {"punctuation", "A/B?", "a-b"},
        {"already clean", "ok", "ok"},
    }
    for _, c := range cases {
        t.Run(c.name, func(t *testing.T) {
            if got := Slugify(c.in); got != c.want {
                t.Errorf("got %q, want %q", got, c.want)
            }
        })
    }
}

More in Testing

Random picks