2017-12-22 16:33:58 -05:00
|
|
|
/*Package assert provides assertions for comparing expected values to actual
|
2022-05-27 12:11:13 -04:00
|
|
|
values in tests. When an assertion fails a helpful error message is printed.
|
2018-01-16 17:20:43 -05:00
|
|
|
|
2022-05-27 12:11:13 -04:00
|
|
|
Example usage
|
2018-01-16 17:20:43 -05:00
|
|
|
|
2022-05-27 12:11:13 -04:00
|
|
|
All the assertions in this package use testing.T.Helper to mark themselves as
|
|
|
|
test helpers. This allows the testing package to print the filename and line
|
|
|
|
number of the file function that failed.
|
2018-01-16 17:20:43 -05:00
|
|
|
|
2022-05-27 12:11:13 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
// filename_test.go:212: assertion failed: error is not nil: file not found
|
2018-01-16 17:20:43 -05:00
|
|
|
|
2022-05-27 12:11:13 -04:00
|
|
|
If any assertion is called from a helper function, make sure to call t.Helper
|
|
|
|
from the helper function so that the filename and line number remain correct.
|
2018-01-16 17:20:43 -05:00
|
|
|
|
2022-05-27 12:11:13 -04:00
|
|
|
The examples below show assert used with some common types and the failure
|
|
|
|
messages it produces. The filename and line number portion of the failure
|
|
|
|
message is omitted from these examples for brevity.
|
2018-01-16 17:20:43 -05:00
|
|
|
|
2022-05-27 12:11:13 -04:00
|
|
|
// booleans
|
2018-01-16 17:20:43 -05:00
|
|
|
|
2022-05-27 12:11:13 -04:00
|
|
|
assert.Assert(t, ok)
|
|
|
|
// assertion failed: ok is false
|
|
|
|
assert.Assert(t, !missing)
|
|
|
|
// assertion failed: missing is true
|
2018-01-16 17:20:43 -05:00
|
|
|
|
2022-05-27 12:11:13 -04:00
|
|
|
// primitives
|
2018-01-16 17:20:43 -05:00
|
|
|
|
2022-05-27 12:11:13 -04:00
|
|
|
assert.Equal(t, count, 1)
|
|
|
|
// assertion failed: 0 (count int) != 1 (int)
|
|
|
|
assert.Equal(t, msg, "the message")
|
|
|
|
// assertion failed: my message (msg string) != the message (string)
|
|
|
|
assert.Assert(t, total != 10) // use Assert for NotEqual
|
|
|
|
// assertion failed: total is 10
|
|
|
|
assert.Assert(t, count > 20, "count=%v", count)
|
|
|
|
// assertion failed: count is <= 20: count=1
|
2018-01-16 17:20:43 -05:00
|
|
|
|
2022-05-27 12:11:13 -04:00
|
|
|
// errors
|
2018-01-16 17:20:43 -05:00
|
|
|
|
2022-05-27 12:11:13 -04:00
|
|
|
assert.NilError(t, closer.Close())
|
|
|
|
// assertion failed: error is not nil: close /file: errno 11
|
|
|
|
assert.Error(t, err, "the exact error message")
|
|
|
|
// assertion failed: expected error "the exact error message", got "oops"
|
|
|
|
assert.ErrorContains(t, err, "includes this")
|
|
|
|
// assertion failed: expected error to contain "includes this", got "oops"
|
|
|
|
assert.ErrorIs(t, err, os.ErrNotExist)
|
|
|
|
// assertion failed: error is "oops", not "file does not exist" (os.ErrNotExist)
|
2018-01-16 17:20:43 -05:00
|
|
|
|
2022-05-27 12:11:13 -04:00
|
|
|
// complex types
|
|
|
|
|
|
|
|
assert.DeepEqual(t, result, myStruct{Name: "title"})
|
|
|
|
// assertion failed: ... (diff of the two structs)
|
|
|
|
assert.Assert(t, is.Len(items, 3))
|
|
|
|
// assertion failed: expected [] (length 0) to have length 3
|
|
|
|
assert.Assert(t, len(sequence) != 0) // use Assert for NotEmpty
|
|
|
|
// assertion failed: len(sequence) is 0
|
|
|
|
assert.Assert(t, is.Contains(mapping, "key"))
|
|
|
|
// assertion failed: map[other:1] does not contain key
|
|
|
|
|
|
|
|
// pointers and interface
|
|
|
|
|
|
|
|
assert.Assert(t, ref == nil)
|
|
|
|
// assertion failed: ref is not nil
|
|
|
|
assert.Assert(t, ref != nil) // use Assert for NotNil
|
|
|
|
// assertion failed: ref is nil
|
|
|
|
|
|
|
|
Assert and Check
|
|
|
|
|
|
|
|
Assert and Check are very similar, they both accept a Comparison, and fail
|
|
|
|
the test when the comparison fails. The one difference is that Assert uses
|
|
|
|
testing.T.FailNow to fail the test, which will end the test execution immediately.
|
|
|
|
Check uses testing.T.Fail to fail the test, which allows it to return the
|
|
|
|
result of the comparison, then proceed with the rest of the test case.
|
|
|
|
|
|
|
|
Like testing.T.FailNow, Assert must be called from the goroutine running the test,
|
|
|
|
not from other goroutines created during the test. Check is safe to use from any
|
|
|
|
goroutine.
|
2018-01-16 17:20:43 -05:00
|
|
|
|
|
|
|
Comparisons
|
|
|
|
|
2020-11-23 06:33:57 -05:00
|
|
|
Package http://pkg.go.dev/gotest.tools/v3/assert/cmp provides
|
2018-01-16 17:20:43 -05:00
|
|
|
many common comparisons. Additional comparisons can be written to compare
|
2017-12-22 16:33:58 -05:00
|
|
|
values in other ways. See the example Assert (CustomComparison).
|
2018-01-16 17:20:43 -05:00
|
|
|
|
2018-06-08 12:09:51 -04:00
|
|
|
Automated migration from testify
|
|
|
|
|
2020-02-07 08:39:24 -05:00
|
|
|
gty-migrate-from-testify is a command which translates Go source code from
|
|
|
|
testify assertions to the assertions provided by this package.
|
2018-06-08 12:09:51 -04:00
|
|
|
|
2020-11-23 06:33:57 -05:00
|
|
|
See http://pkg.go.dev/gotest.tools/v3/assert/cmd/gty-migrate-from-testify.
|
2018-06-08 12:09:51 -04:00
|
|
|
|
|
|
|
|
2018-01-16 17:20:43 -05:00
|
|
|
*/
|
2020-02-07 08:39:24 -05:00
|
|
|
package assert // import "gotest.tools/v3/assert"
|
2018-01-16 17:20:43 -05:00
|
|
|
|
|
|
|
import (
|
2017-12-22 16:33:58 -05:00
|
|
|
gocmp "github.com/google/go-cmp/cmp"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert/cmp"
|
2020-11-23 06:33:57 -05:00
|
|
|
"gotest.tools/v3/internal/assert"
|
2018-01-16 17:20:43 -05:00
|
|
|
)
|
|
|
|
|
2022-05-27 12:11:13 -04:00
|
|
|
// BoolOrComparison can be a bool, cmp.Comparison, or error. See Assert for
|
|
|
|
// details about how this type is used.
|
2018-01-16 17:20:43 -05:00
|
|
|
type BoolOrComparison interface{}
|
|
|
|
|
|
|
|
// TestingT is the subset of testing.T used by the assert package.
|
|
|
|
type TestingT interface {
|
|
|
|
FailNow()
|
|
|
|
Fail()
|
|
|
|
Log(args ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type helperT interface {
|
|
|
|
Helper()
|
|
|
|
}
|
|
|
|
|
2020-02-07 08:39:24 -05:00
|
|
|
// Assert performs a comparison. If the comparison fails, the test is marked as
|
2017-12-22 16:33:58 -05:00
|
|
|
// failed, a failure message is logged, and execution is stopped immediately.
|
|
|
|
//
|
2020-02-07 08:39:24 -05:00
|
|
|
// The comparison argument may be one of three types:
|
2022-05-27 12:11:13 -04:00
|
|
|
//
|
2020-02-07 08:39:24 -05:00
|
|
|
// bool
|
2022-05-27 12:11:13 -04:00
|
|
|
// True is success. False is a failure. The failure message will contain
|
|
|
|
// the literal source code of the expression.
|
|
|
|
//
|
2020-02-07 08:39:24 -05:00
|
|
|
// cmp.Comparison
|
2022-05-27 12:11:13 -04:00
|
|
|
// Uses cmp.Result.Success() to check for success or failure.
|
|
|
|
// The comparison is responsible for producing a helpful failure message.
|
|
|
|
// http://pkg.go.dev/gotest.tools/v3/assert/cmp provides many common comparisons.
|
|
|
|
//
|
2020-02-07 08:39:24 -05:00
|
|
|
// error
|
2022-05-27 12:11:13 -04:00
|
|
|
// A nil value is considered success, and a non-nil error is a failure.
|
|
|
|
// The return value of error.Error is used as the failure message.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Extra details can be added to the failure message using msgAndArgs. msgAndArgs
|
|
|
|
// may be either a single string, or a format string and args that will be
|
|
|
|
// passed to fmt.Sprintf.
|
|
|
|
//
|
|
|
|
// Assert uses t.FailNow to fail the test. Like t.FailNow, Assert must be called
|
|
|
|
// from the goroutine running the test function, not from other
|
|
|
|
// goroutines created during the test. Use Check from other goroutines.
|
2018-01-16 17:20:43 -05:00
|
|
|
func Assert(t TestingT, comparison BoolOrComparison, msgAndArgs ...interface{}) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2020-11-23 06:33:57 -05:00
|
|
|
if !assert.Eval(t, assert.ArgsFromComparisonCall, comparison, msgAndArgs...) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2018-01-16 17:20:43 -05:00
|
|
|
}
|
|
|
|
|
2017-12-22 16:33:58 -05:00
|
|
|
// Check performs a comparison. If the comparison fails the test is marked as
|
2022-05-27 12:11:13 -04:00
|
|
|
// failed, a failure message is printed, and Check returns false. If the comparison
|
|
|
|
// is successful Check returns true. Check may be called from any goroutine.
|
2017-12-22 16:33:58 -05:00
|
|
|
//
|
|
|
|
// See Assert for details about the comparison arg and failure messages.
|
2018-01-16 17:20:43 -05:00
|
|
|
func Check(t TestingT, comparison BoolOrComparison, msgAndArgs ...interface{}) bool {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2020-11-23 06:33:57 -05:00
|
|
|
if !assert.Eval(t, assert.ArgsFromComparisonCall, comparison, msgAndArgs...) {
|
|
|
|
t.Fail()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2018-01-16 17:20:43 -05:00
|
|
|
}
|
|
|
|
|
2022-05-27 12:11:13 -04:00
|
|
|
// NilError fails the test immediately if err is not nil, and includes err.Error
|
|
|
|
// in the failure message.
|
|
|
|
//
|
|
|
|
// NilError uses t.FailNow to fail the test. Like t.FailNow, NilError must be
|
|
|
|
// called from the goroutine running the test function, not from other
|
|
|
|
// goroutines created during the test. Use Check from other goroutines.
|
2018-01-16 17:20:43 -05:00
|
|
|
func NilError(t TestingT, err error, msgAndArgs ...interface{}) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2020-11-23 06:33:57 -05:00
|
|
|
if !assert.Eval(t, assert.ArgsAfterT, err, msgAndArgs...) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2018-01-16 17:20:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Equal uses the == operator to assert two values are equal and fails the test
|
2018-06-08 12:09:51 -04:00
|
|
|
// if they are not equal.
|
|
|
|
//
|
2022-05-27 12:11:13 -04:00
|
|
|
// If the comparison fails Equal will use the variable names and types of
|
|
|
|
// x and y as part of the failure message to identify the actual and expected
|
|
|
|
// values.
|
|
|
|
//
|
|
|
|
// assert.Equal(t, actual, expected)
|
|
|
|
// // main_test.go:41: assertion failed: 1 (actual int) != 21 (expected int32)
|
2018-06-08 12:09:51 -04:00
|
|
|
//
|
|
|
|
// If either x or y are a multi-line string the failure message will include a
|
|
|
|
// unified diff of the two values. If the values only differ by whitespace
|
|
|
|
// the unified diff will be augmented by replacing whitespace characters with
|
|
|
|
// visible characters to identify the whitespace difference.
|
|
|
|
//
|
2022-05-27 12:11:13 -04:00
|
|
|
// Equal uses t.FailNow to fail the test. Like t.FailNow, Equal must be
|
|
|
|
// called from the goroutine running the test function, not from other
|
|
|
|
// goroutines created during the test. Use Check with cmp.Equal from other
|
|
|
|
// goroutines.
|
2018-01-16 17:20:43 -05:00
|
|
|
func Equal(t TestingT, x, y interface{}, msgAndArgs ...interface{}) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2020-11-23 06:33:57 -05:00
|
|
|
if !assert.Eval(t, assert.ArgsAfterT, cmp.Equal(x, y), msgAndArgs...) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2017-12-22 16:33:58 -05:00
|
|
|
}
|
|
|
|
|
2020-02-07 08:39:24 -05:00
|
|
|
// DeepEqual uses google/go-cmp (https://godoc.org/github.com/google/go-cmp/cmp)
|
|
|
|
// to assert two values are equal and fails the test if they are not equal.
|
2018-06-08 12:09:51 -04:00
|
|
|
//
|
2020-11-23 06:33:57 -05:00
|
|
|
// Package http://pkg.go.dev/gotest.tools/v3/assert/opt provides some additional
|
2018-06-08 12:09:51 -04:00
|
|
|
// commonly used Options.
|
|
|
|
//
|
2022-05-27 12:11:13 -04:00
|
|
|
// DeepEqual uses t.FailNow to fail the test. Like t.FailNow, DeepEqual must be
|
|
|
|
// called from the goroutine running the test function, not from other
|
|
|
|
// goroutines created during the test. Use Check with cmp.DeepEqual from other
|
|
|
|
// goroutines.
|
2017-12-22 16:33:58 -05:00
|
|
|
func DeepEqual(t TestingT, x, y interface{}, opts ...gocmp.Option) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2020-11-23 06:33:57 -05:00
|
|
|
if !assert.Eval(t, assert.ArgsAfterT, cmp.DeepEqual(x, y, opts...)) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2017-12-22 16:33:58 -05:00
|
|
|
}
|
|
|
|
|
2022-05-27 12:11:13 -04:00
|
|
|
// Error fails the test if err is nil, or if err.Error is not equal to expected.
|
|
|
|
// Both err.Error and expected will be included in the failure message.
|
|
|
|
// Error performs an exact match of the error text. Use ErrorContains if only
|
|
|
|
// part of the error message is relevant. Use ErrorType or ErrorIs to compare
|
|
|
|
// errors by type.
|
|
|
|
//
|
|
|
|
// Error uses t.FailNow to fail the test. Like t.FailNow, Error must be
|
|
|
|
// called from the goroutine running the test function, not from other
|
|
|
|
// goroutines created during the test. Use Check with cmp.Error from other
|
|
|
|
// goroutines.
|
|
|
|
func Error(t TestingT, err error, expected string, msgAndArgs ...interface{}) {
|
2017-12-22 16:33:58 -05:00
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2022-05-27 12:11:13 -04:00
|
|
|
if !assert.Eval(t, assert.ArgsAfterT, cmp.Error(err, expected), msgAndArgs...) {
|
2020-11-23 06:33:57 -05:00
|
|
|
t.FailNow()
|
|
|
|
}
|
2017-12-22 16:33:58 -05:00
|
|
|
}
|
|
|
|
|
2022-05-27 12:11:13 -04:00
|
|
|
// ErrorContains fails the test if err is nil, or if err.Error does not
|
|
|
|
// contain the expected substring. Both err.Error and the expected substring
|
|
|
|
// will be included in the failure message.
|
|
|
|
//
|
|
|
|
// ErrorContains uses t.FailNow to fail the test. Like t.FailNow, ErrorContains
|
|
|
|
// must be called from the goroutine running the test function, not from other
|
|
|
|
// goroutines created during the test. Use Check with cmp.ErrorContains from other
|
|
|
|
// goroutines.
|
2017-12-22 16:33:58 -05:00
|
|
|
func ErrorContains(t TestingT, err error, substring string, msgAndArgs ...interface{}) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2020-11-23 06:33:57 -05:00
|
|
|
if !assert.Eval(t, assert.ArgsAfterT, cmp.ErrorContains(err, substring), msgAndArgs...) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2017-12-22 16:33:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorType fails the test if err is nil, or err is not the expected type.
|
2022-05-27 12:11:13 -04:00
|
|
|
// Most new code should use ErrorIs instead. ErrorType may be deprecated in the
|
|
|
|
// future.
|
2017-12-22 16:33:58 -05:00
|
|
|
//
|
|
|
|
// Expected can be one of:
|
2022-05-27 12:11:13 -04:00
|
|
|
//
|
2020-02-07 08:39:24 -05:00
|
|
|
// func(error) bool
|
2022-05-27 12:11:13 -04:00
|
|
|
// The function should return true if the error is the expected type.
|
|
|
|
//
|
|
|
|
// struct{} or *struct{}
|
|
|
|
// A struct or a pointer to a struct. The assertion fails if the error is
|
|
|
|
// not of the same type.
|
|
|
|
//
|
|
|
|
// *interface{}
|
|
|
|
// A pointer to an interface type. The assertion fails if err does not
|
|
|
|
// implement the interface.
|
|
|
|
//
|
2020-02-07 08:39:24 -05:00
|
|
|
// reflect.Type
|
2022-05-27 12:11:13 -04:00
|
|
|
// The assertion fails if err does not implement the reflect.Type.
|
|
|
|
//
|
|
|
|
// ErrorType uses t.FailNow to fail the test. Like t.FailNow, ErrorType
|
|
|
|
// must be called from the goroutine running the test function, not from other
|
|
|
|
// goroutines created during the test. Use Check with cmp.ErrorType from other
|
|
|
|
// goroutines.
|
2017-12-22 16:33:58 -05:00
|
|
|
func ErrorType(t TestingT, err error, expected interface{}, msgAndArgs ...interface{}) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2020-11-23 06:33:57 -05:00
|
|
|
if !assert.Eval(t, assert.ArgsAfterT, cmp.ErrorType(err, expected), msgAndArgs...) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2018-01-16 17:20:43 -05:00
|
|
|
}
|
2022-05-27 12:11:13 -04:00
|
|
|
|
|
|
|
// ErrorIs fails the test if err is nil, or the error does not match expected
|
|
|
|
// when compared using errors.Is. See https://golang.org/pkg/errors/#Is for
|
|
|
|
// accepted arguments.
|
|
|
|
//
|
|
|
|
// ErrorIs uses t.FailNow to fail the test. Like t.FailNow, ErrorIs
|
|
|
|
// must be called from the goroutine running the test function, not from other
|
|
|
|
// goroutines created during the test. Use Check with cmp.ErrorIs from other
|
|
|
|
// goroutines.
|
|
|
|
func ErrorIs(t TestingT, err error, expected error, msgAndArgs ...interface{}) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
|
|
|
if !assert.Eval(t, assert.ArgsAfterT, cmp.ErrorIs(err, expected), msgAndArgs...) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|