Documentation
¶
Overview ¶
Package assert provides convenience assert methods to complement the built in go testing library. It's intended to add onto standard Go tests. Example usage:
func TestSomething(t *testing.T) { i, err := doSomething() assert.NoErr(t, err) assert.Equal(t, i, 123, "returned integer") }
Index ¶
- func Equal(t Tester, expected, actual interface{}, noun string)
- func EqualValues(t Tester, expected, actual interface{}, noun string) bool
- func Error(t Tester, expected error, actual error)
- func ExistsError(t Tester, err error, noun string)
- func False(t Tester, b bool, fmtStr string, vals ...interface{})
- func Len(t Tester, object interface{}, length int, noun string) bool
- func Nil(t Tester, i interface{}, noun string)
- func NoError(t Tester, e error)
- func NotEmpty(t Tester, i interface{}, noun string) bool
- func NotEqual(t Tester, expected, actual interface{}, noun string) bool
- func NotEqualValues(t Tester, expected, actual interface{}, noun string) bool
- func NotNil(t Tester, i interface{}, noun string)
- func NotRegexp(t Tester, rx interface{}, str interface{}, noun string) bool
- func Regexp(t Tester, rx interface{}, str interface{}, noun string) bool
- func True(t Tester, b bool, fmtStr string, vals ...interface{})
- type Equaler
- type Tester
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Equal ¶
Equal ensures that the actual value returned from a test was equal to an expected. it uses reflect.DeepEqual to do so. name is used to describe the values being compared. it's used in the error string if actual != expected.
func EqualValues ¶
EqualValues asserts that two objects are equal or convertable to the same types and equal.
assert.EqualValues(t, uint32(123), int32(123))
func Error ¶
Err calls t.Fatalf if expected is not equal to actual. it uses reflect.DeepEqual to determine if the errors are equal
func ExistsError ¶
ExistsErr calls t.Fatalf if err == nil. The message will explain that the error described by noun was nil when it shouldn't have been
func Len ¶
Len asserts that the specified object has specific length. Len also fails if the object has a type that len() not accept.
assert.Len(t, mySlice, 3)
func Nil ¶
Nil uses reflect.DeepEqual(i, nil) to determine if i is nil. if it's not, Nil calls t.Fatalf explaining that the noun i is not nil when it should have been
func NotEmpty ¶
NotEmpty asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either a slice or a channel with len == 0.
if assert.NotEmpty(t, obj) { assert.Equal(t, "two", obj[1]) }
func NotEqual ¶
NotEqual asserts that the specified values are NOT equal.
assert.NotEqual(t, obj1, obj2)
Pointer variable equality is determined based on the equality of the referenced values (as opposed to the memory addresses).
func NotEqualValues ¶
NotEqualValues asserts that two objects are not equal even when converted to the same type
assert.NotEqualValues(t, obj1, obj2)
func NotNil ¶
NotNil uses reflect.DeepEqual(i, nil) to determine if i is nil. if it is, NotNil calls t.Fatalf explaining that the noun i is nil when it shouldn't have been.
func NotRegexp ¶
NotRegexp asserts that a specified regexp does not match a string.
assert.NotRegexp(t, regexp.MustCompile("starts"), "it's starting") assert.NotRegexp(t, "^start", "it's not starting")
Types ¶
type Equaler ¶
Equaler determines if a type is equal to any other type that conforms to Equaler. All types passed to assert.Equal are checked to see if they conform to this interface, and if they do, their Equal function is called to check for their equality. This call is made instead of the call to reflect.DeepEqual
type Tester ¶
type Tester interface {
Fatalf(string, ...interface{})
}
Tester is a stub interface that *testing.T conforms to. It is used in all exported function calls in this assert library so that the library can be tested, or a caller can use a custom testing library. As said before, however, the most widely used implementation of this interface will be *testing.T. Example usage:
func TestSomething(t *testing.T) { assert.Equal(t, "something", "something", "something") }
func WithFrameWrapper ¶
WithFrameWrapper returns the original Tester, wrapped by a frameWrapper that adds context about how many frames to backtrack on the call stack when identifying the source of a failed assertion. If the Tester passed in is already a frameWrapper, the Tester wrapped by that frameWrapper is unwrapped and re-wrapped with updated context.