Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add PanicAssertionFunc #730

Closed
palsivertsen opened this issue Feb 18, 2019 · 3 comments · Fixed by #1337
Closed

Add PanicAssertionFunc #730

palsivertsen opened this issue Feb 18, 2019 · 3 comments · Fixed by #1337

Comments

@palsivertsen
Copy link
Contributor

palsivertsen commented Feb 18, 2019

There's already common function types for the following assertion groups:

These are very useful for doing table driven tests, but seems like there's no PanicAssertionFunc. It could look like this:

type PanicAssertionFunc func(t assert.TestingT, f assert.PanicTestFunc, msgAndArgs ...interface{}) bool
@devdinu
Copy link
Contributor

devdinu commented Mar 13, 2019

There's a function to test whether given function panics.

func (a *Assertions) Panics(f PanicTestFunc, msgAndArgs ...interface{}) bool

This could be of your interest https://godoc.org/github.com/stretchr/testify/assert#Panics.

@palsivertsen
Copy link
Contributor Author

Yes, that's one of the functions that will match the PanicAssertionFunc signature. If you look at the examples for BoolAssertionFunc, ComparisonAssertionFunc, ErrorAssertionFunc and ValueAssertionFunc, I was hoping to do something similar with Panics and NotPanics like so:

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:

$ go test
--- FAIL: TestSomebodyPanic (0.00s)
    --- FAIL: TestSomebodyPanic/fail_no_panic (0.00s)
        main_test.go:23: 
            	Error Trace:	main_test.go:23
            	Error:      	func (assert.PanicTestFunc)(0x678310) should panic
            	            		Panic value:	<nil>
            	Test:       	TestSomebodyPanic/fail_no_panic
    --- FAIL: TestSomebodyPanic/panics (0.00s)
        main_test.go:23: 
            	Error Trace:	main_test.go:23
            	Error:      	func (assert.PanicTestFunc)(0x6782d0) should panic
            	            		Panic value:	<nil>
            	Test:       	TestSomebodyPanic/panics
FAIL
exit status 1
FAIL	test	0.003s

@fahimbagar
Copy link
Contributor

Since I find a similar concern, I have added PR #1337

dolmen pushed a commit that referenced this issue Mar 5, 2024
Add a `PanicAssertionFunc` to ease writing table-driven tests for panic
assertion.

Closes #730
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants