Hacker News new | past | comments | ask | show | jobs | submit login

testify gets basically everything about Go testing wrong. It even takes its arguments in the wrong order.

https://github.com/golang/go/wiki/TestComments#assert-librar...

https://github.com/golang/go/wiki/TestComments#got-before-wa...

They did at least:

https://github.com/golang/go/wiki/TestComments#compare-full-...

Although the diff-ing is significantly more limited than cmp.




Wow when did they add that to TestComments?

I really disagree though. The native testing framework is overly clunky and verbose. It also makes things harder to debug when they fail. Just look at the example it gives! You don't even know why the test failed, it combines 4 different tests in one line. Are you supposed to inspect the entire object that gets printed out to figure it out? How terrible.


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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: