assert library with generics?

262 views
Skip to first unread message

Harmen

unread,
Feb 22, 2024, 3:20:57 AMFeb 22
to golang-nuts
Hi,

anyone has a tip for a nice (small) "assert" test help library which uses generics for the "equals" and "not equals" functions?

testify is the obvious library to use for tests, which works fine, but it would
be nice to have something which uses generics here.

Thanks!

Seth Hoenig

unread,
Feb 22, 2024, 11:55:38 PMFeb 22
to golang-nuts
https://github.com/shoenig/test

We've been using this for a couple years now, and it's been great. I am biased though, for obvious reasons. It makes use of the go-cmp library under the hood for creating legible diffs, and integrates well with protocmp for when you need to work with protobufs. 

Harmen

unread,
Feb 23, 2024, 3:34:43 AMFeb 23
to Seth Hoenig, golang-nuts
On Thu, Feb 22, 2024 at 04:41:53PM -0800, Seth Hoenig wrote:
> https://github.com/shoenig/test
>
> We've been using this for a couple years now, and it's been great. I am
> biased though, for obvious reasons. It makes use of the go-cmp library
> under the hood for creating legible diffs, and integrates well with
> protocmp for when you need to work with protobufs.

That's exactly what I was looking for:

func Eq[A any](t T, exp, val A, settings ...Setting)

Will give it a try, thanks!


>
> On Thursday, February 22, 2024 at 2:20:57 AM UTC-6 Harmen wrote:
>
> > Hi,
> >
> > anyone has a tip for a nice (small) "assert" test help library which uses
> > generics for the "equals" and "not equals" functions?
> >
> > testify is the obvious library to use for tests, which works fine, but it
> > would
> > be nice to have something which uses generics here.
> >
> > Thanks!
> >
> >
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/61fe55d0-8e3d-4dc0-a12e-00f48dbc7856n%40googlegroups.com.

Pierre Durand

unread,
Feb 23, 2024, 6:44:16 PMFeb 23
to golang-nuts

roger peppe

unread,
Feb 26, 2024, 5:53:10 AMFeb 26
to Harmen, golang-nuts
I'm biased because I had a big hand in designing the API, but I get a lot of pleasure from using this package:


It does a lot more than just "equals" and "not equals" but it's still relatively small and low-dependency.
And the error messages when it fails are decent too.

  cheers,
    rog.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Harri L

unread,
Feb 28, 2024, 11:09:02 AMFeb 28
to golang-nuts

I’m biased as well. FWIW, this is a subpackage of the error handling solution, err2.

The assertion pkg is proven to be a precious tool for many projects. It is unique because it can be used both for the test and production code runs. You’ll set, e.g., assert.NotEmpty(s) in whatever function you need. It’ll be checked in test runs and regular main() started runs. And all of this is done automatically.

For instance, if a currently tested function A calls a function B that uses this assertion package and violates assertion, the test fails in function A’s caller. Still, you get the whole location list to the call stack no matter how deep the call stack is to the function B. This works even over Go module boundaries, but you need help with the error result listing, e.g., the Vim plugin.

Naturally, the happy path performance has been necessary, and, e.g., assert.That is the same as if condition.

Br,
-Harri
Reply all
Reply to author
Forward
0 new messages