Skip to content

Commit 660f15d

Browse files
Iaroslav Ciupingeorgelesica-wf
Iaroslav Ciupin
authored andcommitted
Recover panic in suite
1 parent 5b93e2d commit 660f15d

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

suite/suite.go

+11
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,19 @@ func (suite *Suite) Assert() *assert.Assertions {
5555
return suite.Assertions
5656
}
5757

58+
func failOnPanic(t *testing.T) {
59+
r := recover()
60+
if r != nil {
61+
t.Errorf("test panicked: %v", r)
62+
t.FailNow()
63+
}
64+
}
65+
5866
// Run takes a testing suite and runs all of the tests attached
5967
// to it.
6068
func Run(t *testing.T, suite TestingSuite) {
6169
suite.SetT(t)
70+
defer failOnPanic(t)
6271

6372
if setupAllSuite, ok := suite.(SetupAllSuite); ok {
6473
setupAllSuite.SetupSuite()
@@ -84,6 +93,8 @@ func Run(t *testing.T, suite TestingSuite) {
8493
F: func(t *testing.T) {
8594
parentT := suite.T()
8695
suite.SetT(t)
96+
defer failOnPanic(t)
97+
8798
if setupTestSuite, ok := suite.(SetupTestSuite); ok {
8899
setupTestSuite.SetupTest()
89100
}

suite/suite_test.go

+93
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,99 @@ func (s *SuiteRequireTwice) TestRequireTwo() {
4242
r.Equal(1, 2)
4343
}
4444

45+
type panickingSuite struct {
46+
Suite
47+
panicInSetupSuite bool
48+
panicInSetupTest bool
49+
panicInBeforeTest bool
50+
panicInTest bool
51+
panicInAfterTest bool
52+
panicInTearDownTest bool
53+
panicInTearDownSuite bool
54+
}
55+
56+
func (s *panickingSuite) SetupSuite() {
57+
if s.panicInSetupSuite {
58+
panic("oops in setup suite")
59+
}
60+
}
61+
62+
func (s *panickingSuite) SetupTest() {
63+
if s.panicInSetupTest {
64+
panic("oops in setup test")
65+
}
66+
}
67+
68+
func (s *panickingSuite) BeforeTest(_, _ string) {
69+
if s.panicInBeforeTest {
70+
panic("oops in before test")
71+
}
72+
}
73+
74+
func (s *panickingSuite) Test() {
75+
if s.panicInTest {
76+
panic("oops in test")
77+
}
78+
}
79+
80+
func (s *panickingSuite) AfterTest(_, _ string) {
81+
if s.panicInAfterTest {
82+
panic("oops in after test")
83+
}
84+
}
85+
86+
func (s *panickingSuite) TearDownTest() {
87+
if s.panicInTearDownTest {
88+
panic("oops in tear down test")
89+
}
90+
}
91+
92+
func (s *panickingSuite) TearDownSuite() {
93+
if s.panicInTearDownSuite {
94+
panic("oops in tear down suite")
95+
}
96+
}
97+
98+
func TestSuiteRecoverPanic(t *testing.T) {
99+
ok := true
100+
panickingTests := []testing.InternalTest{
101+
{
102+
Name: "TestPanicInSetupSuite",
103+
F: func(t *testing.T) { Run(t, &panickingSuite{panicInSetupSuite: true}) },
104+
},
105+
{
106+
Name: "TestPanicInSetupTest",
107+
F: func(t *testing.T) { Run(t, &panickingSuite{panicInSetupTest: true}) },
108+
},
109+
{
110+
Name: "TestPanicInBeforeTest",
111+
F: func(t *testing.T) { Run(t, &panickingSuite{panicInBeforeTest: true}) },
112+
},
113+
{
114+
Name: "TestPanicInTest",
115+
F: func(t *testing.T) { Run(t, &panickingSuite{panicInTest: true}) },
116+
},
117+
{
118+
Name: "TestPanicInAfterTest",
119+
F: func(t *testing.T) { Run(t, &panickingSuite{panicInAfterTest: true}) },
120+
},
121+
{
122+
Name: "TestPanicInTearDownTest",
123+
F: func(t *testing.T) { Run(t, &panickingSuite{panicInTearDownTest: true}) },
124+
},
125+
{
126+
Name: "TestPanicInTearDownSuite",
127+
F: func(t *testing.T) { Run(t, &panickingSuite{panicInTearDownSuite: true}) },
128+
},
129+
}
130+
131+
require.NotPanics(t, func() {
132+
ok = testing.RunTests(allTestsFilter, panickingTests)
133+
})
134+
135+
assert.False(t, ok)
136+
}
137+
45138
// This suite is intended to store values to make sure that only
46139
// testing-suite-related methods are run. It's also a fully
47140
// functional example of a testing suite, using setup/teardown methods

0 commit comments

Comments
 (0)