How to test panics?
TestingGoTesting Problem Overview
I'm currently pondering how to write tests that check if a given piece of code panicked? I know that Go uses recover
to catch panics, but unlike say, Java code, you can't really specify what code should be skipped in case of a panic or what have you. So if I have a function:
func f(t *testing.T) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
}
}()
OtherFunctionThatPanics()
t.Errorf("The code did not panic")
}
I can't really tell whether OtherFunctionThatPanics
panicked and we recovered, or if the function did not panic at all. How do I specify which code to skip over if there is no panic and which code to execute if there is a panic? How can I check whether there was some panic we recovered from?
Testing Solutions
Solution 1 - Testing
testing
doesn't really have the concept of "success," only failure. So your code above is about right. You might find this style slightly more clear, but it's basically the same thing.
func TestPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
// The following is the code under test
OtherFunctionThatPanics()
}
I generally find testing
to be fairly weak. You may be interested in more powerful testing engines like Ginkgo. Even if you don't want the full Ginkgo system, you can use just its matcher library, Gomega, which can be used along with testing
. Gomega includes matchers like:
Expect(OtherFunctionThatPanics).To(Panic())
You can also wrap up panic-checking into a simple function:
func TestPanic(t *testing.T) {
assertPanic(t, OtherFunctionThatPanics)
}
func assertPanic(t *testing.T, f func()) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
f()
}
Solution 2 - Testing
If you use testify/assert, then it's a one-liner:
func TestOtherFunctionThatPanics(t *testing.T) {
assert.Panics(t, OtherFunctionThatPanics, "The code did not panic")
}
Or, if your OtherFunctionThatPanics
has a signature other than func()
:
func TestOtherFunctionThatPanics(t *testing.T) {
assert.Panics(t, func() { OtherFunctionThatPanics(arg) }, "The code did not panic")
}
If you haven't tried testify yet, then also check out testify/mock. Super simple assertions and mocks.
Solution 3 - Testing
Idiomatic Standard Library Solution
To me, the solution below is easy to read and shows a maintainer the natural code flow under test. Also, it doesn't require a third-party package.
func TestPanic(t *testing.T) {
// No need to check whether `recover()` is nil. Just turn off the panic.
defer func() { _ = recover() }()
OtherFunctionThatPanics()
// Never reaches here if `OtherFunctionThatPanics` panics.
t.Errorf("did not panic")
}
For a more general solution, you can also do it like this:
func TestPanic(t *testing.T) {
shouldPanic(t, OtherFunctionThatPanics)
}
func shouldPanic(t *testing.T, f func()) {
t.Helper()
defer func() { _ = recover() }()
f()
t.Errorf("should have panicked")
}
PS: _ = recover()
is for satisfying the noisy linters that code does not check the error returned from the recover
call—which is totally acceptable in this case.
Using a third-party package for testing in Go takes away the expressiveness of Go tests. It's like using a function to not to use if err != nil
.
Solution 4 - Testing
When looping over multiple test cases I would go for something like this:
package main
import (
"reflect"
"testing"
)
func TestYourFunc(t *testing.T) {
type args struct {
arg1 int
arg2 int
arg3 int
}
tests := []struct {
name string
args args
want []int
wantErr bool
wantPanic bool
}{
//TODO: write test cases
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
r := recover()
if (r != nil) != tt.wantPanic {
t.Errorf("SequenceInt() recover = %v, wantPanic = %v", r, tt.wantPanic)
}
}()
got, err := YourFunc(tt.args.arg1, tt.args.arg2, tt.args.arg3)
if (err != nil) != tt.wantErr {
t.Errorf("YourFunc() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("YourFunc() = %v, want %v", got, tt.want)
}
})
}
}
Solution 5 - Testing
When you need to check the content of the panic, you can typecast the recovered value:
func TestIsAheadComparedToPanicsWithDifferingStreams(t *testing.T) {
defer func() {
err := recover().(error)
if err.Error() != "Cursor: cannot compare cursors from different streams" {
t.Fatalf("Wrong panic message: %s", err.Error())
}
}()
c1 := CursorFromserializedMust("/foo:0:0")
c2 := CursorFromserializedMust("/bar:0:0")
// must panic
c1.IsAheadComparedTo(c2)
}
If the code you're testing does not panic OR panic with an error OR panic with the error message you expect it to, the test will fail (which is what you'd want).
Solution 6 - Testing
In your case you can do:
func f(t *testing.T) {
recovered := func() (r bool) {
defer func() {
if r := recover(); r != nil {
r = true
}
}()
OtherFunctionThatPanics()
// NOT BE EXECUTED IF PANICS
// ....
}
if ! recovered() {
t.Errorf("The code did not panic")
// EXECUTED IF PANICS
// ....
}
}
As a generic panic router function this will also work:
https://github.com/7d4b9/recover
package recover
func Recovered(IfPanic, Else func(), Then func(recover interface{})) (recoverElse interface{}) {
defer func() {
if r := recover(); r != nil {
{
// EXECUTED IF PANICS
if Then != nil {
Then(r)
}
}
}
}()
IfPanic()
{
// NOT BE EXECUTED IF PANICS
if Else != nil {
defer func() {
recoverElse = recover()
}()
Else()
}
}
return
}
var testError = errors.New("expected error")
func TestRecover(t *testing.T) {
Recovered(
func() {
panic(testError)
},
func() {
t.Errorf("The code did not panic")
},
func(r interface{}) {
if err := r.(error); err != nil {
assert.Error(t, testError, err)
return
}
t.Errorf("The code did an unexpected panic")
},
)
}
Solution 7 - Testing
Below is my panic expected
func TestPanic(t *testing.T) {
panicF := func() {
//panic call here
}
require.Panics(t, panicF)
}
Solution 8 - Testing
You can test which function paniced by giving panic an input
package main
import "fmt"
func explode() {
// Cause a panic.
panic("WRONG")
}
func explode1() {
// Cause a panic.
panic("WRONG1")
}
func main() {
// Handle errors in defer func with recover.
defer func() {
if r := recover(); r != nil {
var ok bool
err, ok := r.(error)
if !ok {
err = fmt.Errorf("pkg: %v", r)
fmt.Println(err)
}
}
}()
// These causes an error. change between these
explode()
//explode1()
fmt.Println("Everything fine")
}
Solution 9 - Testing
I would like to
testPanic1
simpletestPanic2
I prefer using this way because it is not enough to expect an error to occur. It should be precisely what the error is.
func testPanic1(testFunc func()) (isPanic bool) {
defer func() {
if err := recover(); err != nil {
isPanic = true
}
}()
testFunc()
return false
}
func TestPanic() {
fmt.Println(testPanic1(func() { panic("error...") })) // true
fmt.Println(testPanic1(func() { fmt.Println("") })) // false
}
func testPanic2(testFunc func()) (reason interface{}, isPanic bool) {
defer func() {
if err := recover(); err != nil {
reason = err
isPanic = true
}
}()
testFunc()
return nil, false
}
func TestPanic2() {
reason, isPanic := testPanic2(func() { panic("my error") })
fmt.Println(reason, isPanic) // "my error", true
reason, isPanic = testPanic2(func() { fmt.Println("") })
fmt.Println(reason, isPanic) // nil, false
}
More example
package _test
import (
"fmt"
"testing"
)
func testPanic(testFunc func()) (reason interface{}, isPanic bool) {
defer func() {
if err := recover(); err != nil {
reason = err
isPanic = true
}
}()
testFunc()
return nil, false
}
func TestPanicFunc(t *testing.T) {
if reason, isPanic := testPanic(func() {
panic("invalid memory address")
}); !isPanic || reason != "invalid memory address" {
t.Fatalf(`did not panic or panic msg != invalid memory address`)
}
if _, isPanic := testPanic(func() {
_ = fmt.Sprintln("hello world")
}); isPanic {
t.Fatalf("It shouldn't cause panic.")
}
var ps *string
if reason, isPanic := testPanic(func() {
fmt.Print(*ps)
}); !isPanic || reason.(error).Error() != "runtime error: invalid memory address or nil pointer dereference" {
t.Fatalf(`did not panic or panic msg != "runtime error: invalid memory address or nil pointer dereference"`)
}
}