📖 12 min read (~ 2600 words).

Error

Asserting Errors

Assertions

GoDoc

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

This domain exposes 10 functionalities. Generic assertions are marked with a . Assertions requiring a newer Go toolchain are marked with a version badge, e.g. go1.26 (the assertion is unavailable on older toolchains).

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"
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestEqualError(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestEqualError(t *testing.T)
    	success := assert.EqualError(t, assert.ErrTest, "assert.ErrTest general error for testing")
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestEqualError(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestEqualError(t *testing.T)
    	require.EqualError(t, require.ErrTest, "assert.ErrTest general error for testing")
    	fmt.Println("passed")
    
    }

Error

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

Examples
	actualObj, err := SomeFunction()
	assertions.Error(t, err)
	success: ErrTest
	failure: nil
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestError(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestError(t *testing.T)
    	success := assert.Error(t, assert.ErrTest)
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestError(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestError(t *testing.T)
    	require.Error(t, require.ErrTest)
    	fmt.Println("passed")
    
    }

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)
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestErrorAs(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestErrorAs(t *testing.T)
    	success := assert.ErrorAs(t, fmt.Errorf("wrap: %w", &dummyError{}), new(*dummyError))
    	fmt.Printf("success: %t\n", success)
    
    }
    
    type dummyError struct {
    }
    
    func (d *dummyError) Error() string {
    	return "dummy error"
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestErrorAs(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestErrorAs(t *testing.T)
    	require.ErrorAs(t, fmt.Errorf("wrap: %w", &dummyError{}), new(*dummyError))
    	fmt.Println("passed")
    
    }
    
    type dummyError struct {
    }
    
    func (d *dummyError) Error() string {
    	return "dummy error"
    }

ErrorAsType[E error] go1.26

ErrorAsType asserts that at least one of the errors in err’s chain is of type E.

It is the type-safe counterpart of ErrorAs, built on the go1.26 errors.AsType: the expected type is the type parameter E (checked at compile time, no reflection), rather than the untyped any target used by ErrorAs.

target receives the matched error when the assertion succeeds. It may be nil, for callers that only want to know whether the chain holds an error of type E: in that case E cannot be inferred and must be supplied explicitly.

This assertion requires go1.26 or newer; it is unavailable on older toolchains.

Examples
	// capture the matched error (E is inferred from target):
	var target *MyError
	assertions.ErrorAsType(t, err, &target)
	// only check, discarding the value (E given explicitly):
	assertions.ErrorAsType[*MyError](t, err, nil)
	success: fmt.Errorf("wrap: %w", &dummyError{}), new(*dummyError)
	failure: ErrTest, new(*dummyError)
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestErrorAsType(t *testing.T)
    t := new(testing.T)
    success := assert.ErrorAsType(t, fmt.Errorf("wrap: %w", &dummyError{}), new(*dummyError))
    fmt.Printf("success: %t\n", success)
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestErrorAsType(t *testing.T)
    t := new(testing.T)
    require.ErrorAsType(t, fmt.Errorf("wrap: %w", &dummyError{}), new(*dummyError))
    fmt.Println("passed")

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"
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestErrorContains(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestErrorContains(t *testing.T)
    	success := assert.ErrorContains(t, assert.ErrTest, "general error")
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestErrorContains(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestErrorContains(t *testing.T)
    	require.ErrorContains(t, require.ErrTest, "general error")
    	fmt.Println("passed")
    
    }

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
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestErrorIs(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"io"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestErrorIs(t *testing.T)
    	success := assert.ErrorIs(t, fmt.Errorf("wrap: %w", io.EOF), io.EOF)
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestErrorIs(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"io"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestErrorIs(t *testing.T)
    	require.ErrorIs(t, fmt.Errorf("wrap: %w", io.EOF), io.EOF)
    	fmt.Println("passed")
    
    }

NoError

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

Examples
	actualObj, err := SomeFunction()
	if assert.NoError(t, err) {
		assertions.Equal(t, expectedObj, actualObj)
	}
	success: nil
	failure: ErrTest
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNoError(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNoError(t *testing.T)
    	success := assert.NoError(t, nil)
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNoError(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNoError(t *testing.T)
    	require.NoError(t, nil)
    	fmt.Println("passed")
    
    }

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)
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotErrorAs(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNotErrorAs(t *testing.T)
    	success := assert.NotErrorAs(t, assert.ErrTest, new(*dummyError))
    	fmt.Printf("success: %t\n", success)
    
    }
    
    type dummyError struct {
    }
    
    func (d *dummyError) Error() string {
    	return "dummy error"
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotErrorAs(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNotErrorAs(t *testing.T)
    	require.NotErrorAs(t, require.ErrTest, new(*dummyError))
    	fmt.Println("passed")
    
    }
    
    type dummyError struct {
    }
    
    func (d *dummyError) Error() string {
    	return "dummy error"
    }

NotErrorAsType[E error] go1.26

NotErrorAsType asserts that none of the errors in err’s chain is of type E.

It is the type-safe counterpart of NotErrorAs, built on the go1.26 errors.AsType.

target is only used to infer the type parameter E and is never assigned; it may be nil, in which case E must be supplied explicitly.

This assertion requires go1.26 or newer; it is unavailable on older toolchains.

Examples
	var target *MyError
	assertions.NotErrorAsType(t, err, &target)
	// or, supplying E explicitly:
	assertions.NotErrorAsType[*MyError](t, err, nil)
	success: ErrTest, new(*dummyError)
	failure: fmt.Errorf("wrap: %w", &dummyError{}), new(*dummyError)
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotErrorAsType(t *testing.T)
    t := new(testing.T)
    success := assert.NotErrorAsType(t, assert.ErrTest, new(*dummyError))
    fmt.Printf("success: %t\n", success)
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotErrorAsType(t *testing.T)
    t := new(testing.T)
    require.NotErrorAsType(t, require.ErrTest, new(*dummyError))
    fmt.Println("passed")

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
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotErrorIs(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"io"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNotErrorIs(t *testing.T)
    	success := assert.NotErrorIs(t, assert.ErrTest, io.EOF)
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotErrorIs(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"io"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNotErrorIs(t *testing.T)
    	require.NotErrorIs(t, require.ErrTest, io.EOF)
    	fmt.Println("passed")
    
    }


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