I've grown quite accustomed to using Hamcrest style matchers in Java. So I looked for something similar in Go. I found github.com/rdrdr/hamcrest, but the last activity was three years ago and when I tried to use it I got errors. (Go has changed) I also found Gomega but for some reason it didn't attract me.
I started to write my own, then stopped myself from getting side tracked from what I was doing. But I kept thinking about it, and ended up writing something very simple.
What I came up with allows you to write assertions like:
Assert(t).That(..., Equals(expected))
and to add more information to the error messages with:
Assert(t).That(..., Equals(expected).Comment("..."))
Where t is the *testing.T that Go's testing framework supplies.
Equals returns a tester function (a closure capturing the expected value). A tester returns "" on success, or else an error message.
That is a method that takes a value of any type (i.e. interface{}) and a tester function, and calls the tester with the value. If the tester returns an error message it calls t.Error
Comment is a method on a tester (taking advantage of Go's ability to define methods on any type, not just on classes). It returns a new tester (a closure capturing the message) that passes success ("") through unchanged, but appends it's message to any error messages.
Taking advantage of Go interfaces, I didn't make the code depend on Go's testing.T type. Instead I defined my own interface with a single Error method (matching the one in testing.T) and made Assert wrap that. So it will work with anything that has a suitable Error method. I didn't have any particular usage in mind for that, but it's a nice way to avoid dependencies.
Initially I wrote Equals using "==". That worked for simple values but not for things like slices. I ended up using reflect.DeepEqual which seems to work. I'm not sure if this is the best approach. Obviously it won't work for things like less than or greater than.
One of the problems I had was that errors would be reported as always occurring on the same line of my hamcrest.go file where I called Error rather than the relevant line in my test. This is a more general problem whenever tests use any kind of helper function that ends up calling Error. Maybe that's not the normal style, but I tend to do it a lot. I found the code where it does this in the decorate method in testing.go but there doesn't appear to be any way to override it. It would be easy enough to modify testing.go, but I'm not sure how I'd get "go test" to use it, short of building a custom version of Go which doesn't seem like a good solution. Ideally Go test would report the portion of the call stack within my code.
I ended up just adding my own reporting. Rather than hard coding how far back in the call stack to go (as testing.go does), I looked for the first call after the testing framework, i.e. the actual top level test function. So errors have the correct location on the end:
hamcrest.go:38: expected 5790 but got 579 {dbldisp_test.go:16}
Obviously, this is not a complete implementation of Hamcrest style matchers. It was a good exercise to explore some of Go's features like interfaces, function value, and closures. I've been using it to write tests but I'm not sure if I'll do more on it and use it longer term or find something else to use.
UPDATE: Something I forgot to mention is that when I'm using this I'm doing:
import . "hamcrest"
The dot allows you to use the exported names from hamcrest (e.g. Assert and Equals) without requiring the package name prefix. This is discouraged, but in this case it seems preferable to writing:
hamcrest.Assert(t).That(..., hamcrest.Equals(expected))
Here's the code on GitHub:
2 comments:
Interesting.. about three years ago I started to do the same thing. I had no idea you had done this, but it looks like we ended up with something similar..
I got a bit further, with a few more matchers. Thought you might be interested?
https://github.com/corbym/gocrest
Nice, thanks.
I only developed the minimum I needed.
I actually switched to a different approach recently:
https://thesoftwarelife.blogspot.com/2020/08/a-new-assert-package-for-go.html
Post a Comment