Documentation ¶
Overview ¶
Package assert provides assertions for comparing expected values to actual values. When an assertion fails a helpful error message is printed.
Assert and Check ¶
Assert() and Check() both accept a Comparison, and fail the test when the comparison fails. The one difference is that Assert() will end the test execution immediately (using t.FailNow()) whereas Check() will fail the test (using t.Fail()), return the value of the comparison, then proceed with the rest of the test case.
Example usage ¶
The example below shows assert used with some common types.
import ( "testing" "github.com/gotestyourself/gotestyourself/assert" is "github.com/gotestyourself/gotestyourself/assert/cmp" ) func TestEverything(t *testing.T) { // booleans assert.Assert(t, ok) assert.Assert(t, !missing) // primitives assert.Equal(t, count, 1) assert.Equal(t, msg, "the message") assert.Assert(t, total != 10) // NotEqual // errors assert.NilError(t, closer.Close()) assert.Error(t, err, "the exact error message") assert.ErrorContains(t, err, "includes this") assert.ErrorType(t, err, os.IsNotExist) // complex types assert.DeepEqual(t, result, myStruct{Name: "title"}) assert.Assert(t, is.Len(items, 3)) assert.Assert(t, len(sequence) != 0) // NotEmpty assert.Assert(t, is.Contains(mapping, "key")) // pointers and interface assert.Assert(t, is.Nil(ref)) assert.Assert(t, ref != nil) // NotNil }
Comparisons ¶
Package assert/cmp (http://bit.do/assert-cmp) provides many common comparisons. Additional comparisons can be written to compare values in other ways. See the example Assert (CustomComparison).
Automated migration from testify ¶
gty-migrate-from-testify is a binary which can update source code which uses testify assertions to use the assertions provided by this package.
Index ¶
- func Assert(t TestingT, comparison BoolOrComparison, msgAndArgs ...interface{})
- func Check(t TestingT, comparison BoolOrComparison, msgAndArgs ...interface{}) bool
- func DeepEqual(t TestingT, x, y interface{}, opts ...gocmp.Option)
- func Equal(t TestingT, x, y interface{}, msgAndArgs ...interface{})
- func Error(t TestingT, err error, message string, msgAndArgs ...interface{})
- func ErrorContains(t TestingT, err error, substring string, msgAndArgs ...interface{})
- func ErrorType(t TestingT, err error, expected interface{}, msgAndArgs ...interface{})
- func NilError(t TestingT, err error, msgAndArgs ...interface{})
- type BoolOrComparison
- type TestingT
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Assert ¶
func Assert(t TestingT, comparison BoolOrComparison, msgAndArgs ...interface{})
Assert performs a comparison. If the comparison fails the test is marked as failed, a failure message is logged, and execution is stopped immediately.
The comparison argument may be one of three types: bool, cmp.Comparison or error. When called with a bool the failure message will contain the literal source code of the expression. When called with a cmp.Comparison the comparison is responsible for producing a helpful failure message. When called with an error a nil value is considered success. A non-nil error is a failure, and Error() is used as the failure message.
Example (CustomComparison) ¶
package main import ( "fmt" "regexp" "testing" "github.com/gotestyourself/gotestyourself/assert" "github.com/gotestyourself/gotestyourself/assert/cmp" ) var t = &testing.T{} func main() { regexPattern := func(value string, pattern string) cmp.Comparison { return func() cmp.Result { re := regexp.MustCompile(pattern) if re.MatchString(value) { return cmp.ResultSuccess } return cmp.ResultFailure( fmt.Sprintf("%q did not match pattern %q", value, pattern)) } } assert.Assert(t, regexPattern("12345.34", `\d+.\d\d`)) }
Output:
func Check ¶
func Check(t TestingT, comparison BoolOrComparison, msgAndArgs ...interface{}) bool
Check performs a comparison. If the comparison fails the test is marked as failed, a failure message is logged, and Check returns false. Otherwise returns true.
See Assert for details about the comparison arg and failure messages.
func DeepEqual ¶
DeepEqual uses google/go-cmp (http://bit.do/go-cmp) to assert two values are equal and fails the test if they are not equal.
Package assert/opt (http://bit.do/t-assert-opt) provides some additional commonly used Options.
This is equivalent to Assert(t, cmp.DeepEqual(x, y)).
func Equal ¶
func Equal(t TestingT, x, y interface{}, msgAndArgs ...interface{})
Equal uses the == operator to assert two values are equal and fails the test if they are not equal. This is equivalent to Assert(t, cmp.Equal(x, y)).
func Error ¶ added in v1.4.0
Error fails the test if err is nil, or the error message is not the expected message. Equivalent to Assert(t, cmp.Error(err, message)).
func ErrorContains ¶ added in v1.4.0
ErrorContains fails the test if err is nil, or the error message does not contain the expected substring. Equivalent to Assert(t, cmp.ErrorContains(err, substring)).
func ErrorType ¶ added in v1.4.0
ErrorType fails the test if err is nil, or err is not the expected type.
Expected can be one of: a func(error) bool which returns true if the error is the expected type, an instance of (or a pointer to) a struct of the expected type, a pointer to an interface the error is expected to implement, a reflect.Type of the expected struct or interface.
Equivalent to Assert(t, cmp.ErrorType(err, expected)).
Types ¶
type BoolOrComparison ¶
type BoolOrComparison interface{}
BoolOrComparison can be a bool, or cmp.Comparison. See Assert() for usage.
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
gty-migrate-from-testify
Command gty-migrate-from-testify migrates packages from testify/assert and testify/require to gotestyourself/assert.
|
Command gty-migrate-from-testify migrates packages from testify/assert and testify/require to gotestyourself/assert. |
Package cmp provides Comparisons for Assert and Check
|
Package cmp provides Comparisons for Assert and Check |
Package opt provides common go-cmp.Options for use with assert.DeepEqual.
|
Package opt provides common go-cmp.Options for use with assert.DeepEqual. |