Skip to content

Commit

Permalink
suite: correctly set stats on test panic (#1195)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoche authored Jun 23, 2022
1 parent ba1076d commit 1b73601
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
15 changes: 11 additions & 4 deletions suite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ func (suite *Suite) Assert() *assert.Assertions {
return suite.Assertions
}

func failOnPanic(t *testing.T) {
func recoverAndFailOnPanic(t *testing.T) {
r := recover()
failOnPanic(t, r)
}

func failOnPanic(t *testing.T, r interface{}) {
if r != nil {
t.Errorf("test panicked: %v\n%s", r, debug.Stack())
t.FailNow()
Expand All @@ -91,7 +95,7 @@ func (suite *Suite) Run(name string, subtest func()) bool {
// Run takes a testing suite and runs all of the tests attached
// to it.
func Run(t *testing.T, suite TestingSuite) {
defer failOnPanic(t)
defer recoverAndFailOnPanic(t)

suite.SetT(t)

Expand Down Expand Up @@ -136,10 +140,12 @@ func Run(t *testing.T, suite TestingSuite) {
F: func(t *testing.T) {
parentT := suite.T()
suite.SetT(t)
defer failOnPanic(t)
defer recoverAndFailOnPanic(t)
defer func() {
r := recover()

if stats != nil {
passed := !t.Failed()
passed := !t.Failed() && r == nil
stats.end(method.Name, passed)
}

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

suite.SetT(parentT)
failOnPanic(t, r)
}()

if setupTestSuite, ok := suite.(SetupTestSuite); ok {
Expand Down
29 changes: 23 additions & 6 deletions suite/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,19 +501,36 @@ func (s *suiteWithStats) TestSomething() {
s.Equal(1, 1)
}

func (s *suiteWithStats) TestPanic() {
panic("oops")
}

func TestSuiteWithStats(t *testing.T) {
suiteWithStats := new(suiteWithStats)
Run(t, suiteWithStats)

testing.RunTests(allTestsFilter, []testing.InternalTest{
{
Name: "WithStats",
F: func(t *testing.T) {
Run(t, suiteWithStats)
},
},
})

assert.True(t, suiteWithStats.wasCalled)
assert.NotZero(t, suiteWithStats.stats.Start)
assert.NotZero(t, suiteWithStats.stats.End)
assert.True(t, suiteWithStats.stats.Passed())
assert.False(t, suiteWithStats.stats.Passed())

testStats := suiteWithStats.stats.TestStats

assert.NotZero(t, testStats["TestSomething"].Start)
assert.NotZero(t, testStats["TestSomething"].End)
assert.True(t, testStats["TestSomething"].Passed)

testStats := suiteWithStats.stats.TestStats["TestSomething"]
assert.NotZero(t, testStats.Start)
assert.NotZero(t, testStats.End)
assert.True(t, testStats.Passed)
assert.NotZero(t, testStats["TestPanic"].Start)
assert.NotZero(t, testStats["TestPanic"].End)
assert.False(t, testStats["TestPanic"].Passed)
}

// FailfastSuite will test the behavior when running with the failfast flag
Expand Down

0 comments on commit 1b73601

Please sign in to comment.