A simple assertion library using Go generics
This library is inspired by testify/require, but with a significantly reduced API surface based on empirical use of that package.
It also provides much nicer diff output, eg.
=== RUN TestFail
assert_test.go:14: Expected values to be equal:
assert.Data{
- Str: "foo",
+ Str: "far",
Num: 10,
}
--- FAIL: TestFail (0.00s)Import then use as assert:
import "github.com/alecthomas/assert/v2"This library has the following API. For all functions, msgAndArgs is used to
format error messages using the fmt package.
…Our empirical data of testify usage comes from a monorepo with around 50K lines of tests.
These are the usage counts for all testify functions, normalised to the base
(not Printf()) non-negative(not No(t)?) case for each core function.
2240 Error
1314 Equal
219 True
210 Nil
167 Empty
107 Contains
79 Len
61 False
24 EqualValues
20 EqualError
17 Zero
15 Fail
15 ElementsMatch
9 Panics
7 IsType
6 FileExists
4 JSONEq
3 PanicsWithValue
3 EventuallyThe decision for each function was:
Error(t, err) -> frequently used, keepEqual(t, expected, actual) -> frequently used, keep but make type safeTrue(t, expr) -> frequently used, keepFalse(t, expr) -> frequently used, keepEmpty(t, thing) -> require.Equal(t, len(thing), 0)Contains(t, haystack string, needle string) - the only variant used in our codebase, keep as concrete typeZero(t, value) -> make type safe, keepPanics(t, f) -> useful, keepEqualError(t, a, b) -> useful, keepNil(t, value) -> frequently used, keepElementsMatch(t, a, b) - use peterrk/slices or stdlib sort support once it lands.IsType(t, a, b) -> require.Equal(t, reflect.TypeOf(a).String(), reflect.TypeOf(b).String())FileExists() -> very little use, dropJSONEq() -> very little use, dropPanicsWithValue() -> very little use, dropEventually() -> very little use, dropContains(t, haystack []T, needle T) - very little use, replace withContains(t, haystack map[K]V, needle K) - very little use, dropLen(t, v, n) -> cannot be implemented as a single function with genericsEqual(t, len(v), n)EqualValues() - Equal(t, TYPE(a), TYPE(b))Fail() -> t.Fatal()No open issues yet, or sync has not completed.