Skip to content
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 #1337

Merged
merged 3 commits into from
Mar 5, 2024
Merged

Conversation

fahimbagar
Copy link
Contributor

@fahimbagar fahimbagar commented Feb 1, 2023

Summary

Add a PanicAssertionFunc to ease writing table-driven tests for panic assertion.

Changes

  • Add PanicAssertionFunc for table driven.

Motivation

  • My team uses panic assertion a lot 😄
  • Useful for building panic assertion
  • There is an issue reported for this one as well: Add PanicAssertionFunc #730
func ExamplePanicAssertionFunc() {
	t := &testing.T{} // provided by test

	tests := []struct {
		name      string
		panicFn   func()
		assertion PanicAssertionFunc
	}{
		{"with panic", func() { panic(nil) }, Panics},
		{"without panic", func() {}, NotPanics},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			tt.assertion(t, tt.panicFn)
		})
	}
}

https://github.com/fahimbagar/testify/blob/cb954e70769531b0462ae2f713c12c74e64ad69c/assert/assertions_test.go#L2410-L2427

Related issues

Closes #730

@fahimbagar fahimbagar mentioned this pull request Feb 1, 2023
@dolmen dolmen added the pkg-assert Change related to package testify/assert label Jul 6, 2023
assert/assertions.go Outdated Show resolved Hide resolved

tests := []struct {
name string
panicFn func()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type should be PanicTestFunc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebased and updated the type

assert/assertions_test.go Outdated Show resolved Hide resolved
@fahimbagar
Copy link
Contributor Author

Thanks @dolmen

@dolmen dolmen merged commit 8585d8d into stretchr:master Mar 5, 2024
7 checks passed
@dolmen
Copy link
Collaborator

dolmen commented Mar 5, 2024

Thanks @fahimbagar ! 🎉

@fahimbagar fahimbagar deleted the PanicAssertionFunc branch March 5, 2024 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement pkg-assert Change related to package testify/assert
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add PanicAssertionFunc
3 participants