The 9 Go test assertions I use (and why)
11 points by carlana
11 points by carlana
My own assertion library was inspired by Alex Edwards, so I am inclined to agree with his stylistic choices here.
IMO, to the first approximation, if you need something beyond fn assert(bool)
, you should use inline snapshot test: https://ianthehenry.com/posts/my-kind-of-repl/
That’s basically what I do https://dev.to/yawaramin/why-i-dont-use-a-third-party-assertion-library-in-go-unit-tests-1gak
“much better than using an assert.True() helper and getting a failure message like this”
testify/assert
allows a custom failure message for every assertion.
assert.True(t, is42(43), "43 is right out.")
Will give something like
--- FAIL: Test_is42 (0.00s)
main_test.go:12:
Error Trace: /Users/rjp/tmp/gp/main_test.go:12
Error: Should be true
Test: Test_is42
Messages: 43 is right out.
FAIL
exit status 1
FAIL gp 0.164s
A lot of interesting thoughts here. Go is not my primary language, so I reached out for assertions libraries because everyone did.
My only remark, i would insist not t use “NotNil()” assertion and always replace it with Equal() assertion.