Skip to content

Commit 1b73601

Browse files
authored
suite: correctly set stats on test panic (#1195)
1 parent ba1076d commit 1b73601

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

suite/suite.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ func (suite *Suite) Assert() *assert.Assertions {
6767
return suite.Assertions
6868
}
6969

70-
func failOnPanic(t *testing.T) {
70+
func recoverAndFailOnPanic(t *testing.T) {
7171
r := recover()
72+
failOnPanic(t, r)
73+
}
74+
75+
func failOnPanic(t *testing.T, r interface{}) {
7276
if r != nil {
7377
t.Errorf("test panicked: %v\n%s", r, debug.Stack())
7478
t.FailNow()
@@ -91,7 +95,7 @@ func (suite *Suite) Run(name string, subtest func()) bool {
9195
// Run takes a testing suite and runs all of the tests attached
9296
// to it.
9397
func Run(t *testing.T, suite TestingSuite) {
94-
defer failOnPanic(t)
98+
defer recoverAndFailOnPanic(t)
9599

96100
suite.SetT(t)
97101

@@ -136,10 +140,12 @@ func Run(t *testing.T, suite TestingSuite) {
136140
F: func(t *testing.T) {
137141
parentT := suite.T()
138142
suite.SetT(t)
139-
defer failOnPanic(t)
143+
defer recoverAndFailOnPanic(t)
140144
defer func() {
145+
r := recover()
146+
141147
if stats != nil {
142-
passed := !t.Failed()
148+
passed := !t.Failed() && r == nil
143149
stats.end(method.Name, passed)
144150
}
145151

@@ -152,6 +158,7 @@ func Run(t *testing.T, suite TestingSuite) {
152158
}
153159

154160
suite.SetT(parentT)
161+
failOnPanic(t, r)
155162
}()
156163

157164
if setupTestSuite, ok := suite.(SetupTestSuite); ok {

suite/suite_test.go

+23-6
Original file line numberDiff line numberDiff line change
@@ -501,19 +501,36 @@ func (s *suiteWithStats) TestSomething() {
501501
s.Equal(1, 1)
502502
}
503503

504+
func (s *suiteWithStats) TestPanic() {
505+
panic("oops")
506+
}
507+
504508
func TestSuiteWithStats(t *testing.T) {
505509
suiteWithStats := new(suiteWithStats)
506-
Run(t, suiteWithStats)
510+
511+
testing.RunTests(allTestsFilter, []testing.InternalTest{
512+
{
513+
Name: "WithStats",
514+
F: func(t *testing.T) {
515+
Run(t, suiteWithStats)
516+
},
517+
},
518+
})
507519

508520
assert.True(t, suiteWithStats.wasCalled)
509521
assert.NotZero(t, suiteWithStats.stats.Start)
510522
assert.NotZero(t, suiteWithStats.stats.End)
511-
assert.True(t, suiteWithStats.stats.Passed())
523+
assert.False(t, suiteWithStats.stats.Passed())
524+
525+
testStats := suiteWithStats.stats.TestStats
526+
527+
assert.NotZero(t, testStats["TestSomething"].Start)
528+
assert.NotZero(t, testStats["TestSomething"].End)
529+
assert.True(t, testStats["TestSomething"].Passed)
512530

513-
testStats := suiteWithStats.stats.TestStats["TestSomething"]
514-
assert.NotZero(t, testStats.Start)
515-
assert.NotZero(t, testStats.End)
516-
assert.True(t, testStats.Passed)
531+
assert.NotZero(t, testStats["TestPanic"].Start)
532+
assert.NotZero(t, testStats["TestPanic"].End)
533+
assert.False(t, testStats["TestPanic"].Passed)
517534
}
518535

519536
// FailfastSuite will test the behavior when running with the failfast flag

0 commit comments

Comments
 (0)