Simple and lightweight testing assertion library for Go.
go get github.com/gravitton/assertpackage foo
import (
"testing"
"github.com/gravitton/assert"
)
func TestFoo(t *testing.T) {
assert.Equal(t, actual, expected)
assert.Contains(t, []int{1, 2, 3}, 2)
assert.NoError(t, err)
assert.JSON(t, obj, `{"data":1}`)
}| Function | Description |
|---|---|
True(t, condition) |
condition is true |
False(t, condition) |
condition is false |
Equal(t, actual, expected) |
values are equal (deep) |
NotEqual(t, actual, expected) |
values are not equal |
EqualDelta(t, actual, expected, delta) |
numeric values differ by at most delta |
Same(t, actual, expected) |
pointers reference the same object |
NotSame(t, actual, expected) |
pointers reference different objects |
Length(t, object, n) |
string/slice/map/channel has length n |
Contains(t, object, element) |
string/slice/map/channel contains element |
NotContains(t, object, element) |
string/slice/map/channel does not contain element |
Error(t, err) |
error is not nil |
NoError(t, err) |
error is nil |
ErrorIs(t, err, target) |
error unwraps to target |
NotErrorIs(t, err, target) |
error does not unwrap to target |
EqualJSON(t, actual, expected) |
JSON strings are semantically equal |
JSON(t, object, expected) |
object marshals to expected JSON string |
Fail(t, message) |
always fails with message |
Failf(t, format, args...) |
always fails with formatted message |
All assertions return bool: true on success, false on failure.
An optional trailing messages ...string argument is available on every assertion. The strings are concatenated and prepended to the failure message, which is useful for building custom assertions.
The return value and optional message prefix make it straightforward to compose reusable assertion helpers:
func TestRect(t *testing.T) {
assertRect(t, image.Rect(1, 2, 3, 4), image.Rect(1, 3, 2, 4))
// test.go:4: Min.Y: Should be equal:
// actual: 2
// expected: 3
// test.go:4: Max.X: Should be equal:
// actual: 3
// expected: 2
}
func assertRect(t *testing.T, actual, expected image.Rectangle) bool {
t.Helper()
ok := assertPoint(t, actual.Min, expected.Min, "Min.")
ok = assertPoint(t, actual.Max, expected.Max, "Max.") && ok
return ok
}
func assertPoint(t *testing.T, actual, expected image.Point, messages ...string) bool {
t.Helper()
ok := assert.Equal(t, actual.X, expected.X, append(messages, "X: ")...)
ok = assert.Equal(t, actual.Y, expected.Y, append(messages, "Y: ")...) && ok
return ok
}The MIT License (MIT). Please see License File for more information.