-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PanicAssertionFunc #730
Comments
There's a function to test whether given function panics.
This could be of your interest https://godoc.org/github.com/stretchr/testify/assert#Panics. |
Yes, that's one of the functions that will match the package main
import "github.com/stretchr/testify/assert"
import "testing"
// This is the type I suggest adding to testify
type PanicAssertionFunc func(t assert.TestingT, f assert.PanicTestFunc, msgAndArgs ...interface{}) bool
// This is an example of PanicAssertionFunc usage with table driven tests
func TestSomebodyPanic(t *testing.T) {
tests := map[string]struct {
f func()
assertion PanicAssertionFunc
}{
"panics": {IPanic, assert.Panics},
"no panic": {ImOK, assert.NotPanics},
"fail panic": {IPanic, assert.NotPanics},
"fail no panic": {ImOK, assert.Panics},
}
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
tt.assertion(t, tt.f)
})
}
}
// Helper functions
// IPanic always panics with a nil value
func IPanic() {
panic(nil)
}
// ImOK does nothing
func ImOK() {} The above test will give the following output:
|
Since I find a similar concern, I have added PR #1337 |
There's already common function types for the following assertion groups:
assert.BoolAssertionFunc
assert.ComparisonAssertionFunc
assert.ErrorAssertionFunc
assert.ValueAssertionFunc
These are very useful for doing table driven tests, but seems like there's no
PanicAssertionFunc
. It could look like this:The text was updated successfully, but these errors were encountered: