📖 5 min read (~ 1000 words).

Panic

Asserting A Panic Behavior

Assertions

GoDoc

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

This domain exposes 4 functionalities.

NotPanics

NotPanics asserts that the code inside the specified function does NOT panic.

Examples
	assertions.NotPanics(t, func(){ RemainCalm() })
	success: func() { }
	failure: func() { panic("panicking") }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotPanics(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 TestNotPanics(t *testing.T)
    	success := assert.NotPanics(t, func() {
    	})
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotPanics(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 TestNotPanics(t *testing.T)
    	require.NotPanics(t, func() {
    	})
    	fmt.Println("passed")
    
    }

Panics

Panics asserts that the code inside the specified function panics.

Examples
	assertions.Panics(t, func(){ GoCrazy() })
	success: func() { panic("panicking") }
	failure: func() { }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestPanics(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 TestPanics(t *testing.T)
    	success := assert.Panics(t, func() {
    		panic("panicking")
    	})
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestPanics(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 TestPanics(t *testing.T)
    	require.Panics(t, func() {
    		panic("panicking")
    	})
    	fmt.Println("passed")
    
    }

PanicsWithError

PanicsWithError asserts that the code inside the specified function panics, and that the recovered panic value is an error that satisfies the EqualError comparison.

Examples
	assertions.PanicsWithError(t, "crazy error", func(){ GoCrazy() })
	success: ErrTest.Error(), func() { panic(ErrTest) }
	failure: ErrTest.Error(), func() { }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestPanicsWithError(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 TestPanicsWithError(t *testing.T)
    	success := assert.PanicsWithError(t, assert.ErrTest.Error(), func() {
    		panic(assert.ErrTest)
    	})
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

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

PanicsWithValue

PanicsWithValue asserts that the code inside the specified function panics, and that the recovered panic value equals the expected panic value.

Examples
	assertions.PanicsWithValue(t, "crazy error", func(){ GoCrazy() })
	success: "panicking", func() { panic("panicking") }
	failure: "panicking", func() { }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestPanicsWithValue(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 TestPanicsWithValue(t *testing.T)
    	success := assert.PanicsWithValue(t, "panicking", func() {
    		panic("panicking")
    	})
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestPanicsWithValue(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 TestPanicsWithValue(t *testing.T)
    	require.PanicsWithValue(t, "panicking", func() {
    		panic("panicking")
    	})
    	fmt.Println("passed")
    
    }


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