Takahiro Kudo
Posted on May 27, 2020
I usually test "panic" with below code.
Is it redundant?
// 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 of test.
func TestIsTestCallPanic(t *testing.T) {
isCalled := IsTestCallPanic(func() {
var i interface{}
if i == nil {
panic("Hi, panic.")
}
})
if !isCalled {
t.Errorf("failed.")
}
}
💖 💪 🙅 🚩
Takahiro Kudo
Posted on May 27, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
testing What We Learned From Analyzing 20.2 Million CI Jobs In Trunk Flaky Tests - Part 2
November 27, 2024
undefined How to Use Codecov: Track Test Coverage with GitHub Actions in Your CI Pipeline (Step-by-Step Guide)
November 26, 2024