Skip to content

Instantly share code, notes, and snippets.

@takakd
Created May 27, 2020 14:53
Show Gist options
  • Save takakd/d1be73e7ad237b8c97341cf3fe269075 to your computer and use it in GitHub Desktop.
Save takakd/d1be73e7ad237b8c97341cf3fe269075 to your computer and use it in GitHub Desktop.
Testing panic function.
// Testing if testFunc calls panic.
// e.g.
// IsTestCallPanic(func(){
// <place test target here.>
// })
func IsTestCallPanic(testFunc func()) (ok bool){
defer func() {
if err := recover(); err == nil {
ok = false
}
}()
ok = true
testFunc()
return
}
// Test Code.
func TestIsTestCallPanic(t *testing.T) {
isCalled := IsTestCallPanic(func() {
var i interface{}
if i == nil {
panic("Hi, panic.")
}
})
if !isCalled {
t.Errorf("failed.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment