Eqtest provides equality test assertions API using go-cmp. It is not designed to compete or replace existing assertion frameworks such as testify, but rather to complement where it lacks such as proper equality comparison for complex types.
To start using eqtest
, simply call eqtest.New
from your Go test files:
package mypkg_test
import (
"testing"
"github.com/adzil/eqtest"
)
func TestSomething_Simple(t *testing.T) {
eqtest.Equal(t, "hello", "world") // This should fail the test.
}
func TestSomething_WithNew(t *testing.T) {
eqt := eqtest.New(t)
eqt.Equal("hello", "world") // This should fail the test.
}
There are three ways to add cmp.Option
during the assertion:
This will be useful if all of the assertions require the options such as transformers.
eqtest.New(t, cmp.Transformer(...))
This will be useful if there are subset of the assertions that need to have the same options such as filters.
eqt := eqtest.New(t).With(cmpopts.IgnoreFields(MyStruct{}, "FieldOne"))
eqt.Equal(...)
eqt.Equal(...)
This will be useful if an assertion needs a specific filter that doesn't needed by the others.
eqtest.New(t).Equal(expected, actual,
cmpopts.IgnoreFields(MyStruct{}, "FieldTwo"),
)
If there are subtests that sharing the same cmp.Option
as the current test, the existing Assertion
can be cloned easily using a different *testing.T
by calling Assertion.Using
:
func TestSomething(t *testing.T) {
eqt := eqtest.New(t,
cmpopts.SomeOption(...),
cmpopts.SomeOption(...),
)
t.Run("subtest with identical cmp options", func(t *testing.T) {
eqt := eqt.With(t)
// Now the eqt will refer to the subtest t instead of the parent t.
eqt.Equal(...)
})
}