4 ms·
Yeah, it's a really bad example. I would just cmp.Diff, and if I only cared about 4 fields, I would delete the data from got.FieldIDontCareAbout before compari
by jrockway 6y ago
Yeah, it's a really bad example. I would just cmp.Diff, and if I only cared about 4 fields, I would delete the data from got.FieldIDontCareAbout before comparison. (You can also make cmp.Diff do this if you want to do it every time.)
I think that an evolution of tests would be:
assert.Equals(got.Bar, want.Bar)
assert.Equals(got.Baz, want.Baz)
then:
if got.Bar != want.Bar || got.Baz != want.Baz {
t.Fatalf("bar and baz:\n got: %v\n want: %v", got, want)
}
then:
want := Thing{Bar: xxx, Baz: yyy}
got.Quux = ""
if diff := cmp.Diff(got, want); diff != "" {
t.Fatal("bar and baz:\n%s", diff)
}
(cmp.Ignore and friends should also be considered for less misleading output, if you don't care about Quux)
Basically, cmp.Diff should be your go-to testing framework. It's easy to use and the output is excellent.