📖 10 min read (~ 2000 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"
  • 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"
    }

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"
    }

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