/assert-go

Test assertion library.

Primary LanguageGoMIT LicenseMIT

assert-go

CircleCI Go Report Card GoDoc go.dev

Package assert simplifies writing test assertions.

Output will contain a helpful diff rendered using as well as the source code of the expression being tested. For example, if you call assert.Equal(t, car.Name, "Porsche"), the error message will include "car.Name".

Additional options and custom comparators can be registered using RegisterOptions, or passed in as the last parameter to the function call. For example, to indicate that unexported fields should be ignored on MyType, you can use:

 assert.RegisterOptions(
     cmpopts.IgnoreUnexported(MyType{}),
 )

See the go-cmp docs for more options.

Usage

func Test(t *testing.T) {
    message := "foo"
    assert.Equal(t, message, "bar")
    // message (-got +want):  string(
    // - 	"foo",
    // + 	"bar",
    //   )
    p := Person{Name: "Alice"}
    assert.Equal(t, p, Person{Name: "Bob"})
    // p (-got +want): domain_test.Person{
    // - 	Name: "Alice",
    // + 	Name: "Bob",
    //  }
}