📖 6 min read (~ 1200 words).

Error

Asserting Errors

Assertions

GoDoc

All links point to https://pkg.go.dev/github.com/go-openapi/testify/v2

This domain exposes 8 functionalities.

EqualError

EqualError asserts that a function returned a non-nil error (i.e. an error) and that it is equal to the provided error.

Examples
	actualObj, err := SomeFunction()
	assertions.EqualError(t, err,  expectedErrorString)
	success: ErrTest, "assert.ErrTest general error for testing"
	failure: ErrTest, "wrong error message"

Error

Error asserts that a function returned a non-nil error (ie. an error).

Examples
	actualObj, err := SomeFunction()
	assertions.Error(t, err)
	success: ErrTest
	failure: nil

ErrorAs

ErrorAs asserts that at least one of the errors in err’s chain matches target, and if so, sets target to that error value.

This is a wrapper for errors.As.

Examples
	assertions.ErrorAs(t, err, &target)
	success: fmt.Errorf("wrap: %w", &dummyError{}), new(*dummyError)
	failure: ErrTest, new(*dummyError)

ErrorContains

ErrorContains asserts that a function returned a non-nil error (i.e. an error) and that the error contains the specified substring.

Examples
	actualObj, err := SomeFunction()
	assertions.ErrorContains(t, err,  expectedErrorSubString)
	success: ErrTest, "general error"
	failure: ErrTest, "not in message"

ErrorIs

ErrorIs asserts that at least one of the errors in err’s chain matches target.

This is a wrapper for errors.Is.

Examples
	assertions.ErrorIs(t, err, io.EOF)
	success: fmt.Errorf("wrap: %w", io.EOF), io.EOF
	failure: ErrTest, io.EOF

NoError

NoError asserts that a function returned a nil error (ie. no error).

Examples
	actualObj, err := SomeFunction()
	if assert.NoError(t, err) {
		assertions.Equal(t, expectedObj, actualObj)
	}
	success: nil
	failure: ErrTest

NotErrorAs

NotErrorAs asserts that none of the errors in err’s chain matches target, but if so, sets target to that error value.

Examples
	assertions.NotErrorAs(t, err, &target)
	success: ErrTest, new(*dummyError)
	failure: fmt.Errorf("wrap: %w", &dummyError{}), new(*dummyError)

NotErrorIs

NotErrorIs asserts that none of the errors in err’s chain matches target.

This is a wrapper for errors.Is.

Examples
	assertions.NotErrorIs(t, err, io.EOF)
	success: ErrTest, io.EOF
	failure: fmt.Errorf("wrap: %w", io.EOF), io.EOF


Generated with github.com/go-openapi/testify/v2/codegen