Skip to content

Commit

Permalink
suite: correctly set stats on test panic
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoche committed May 31, 2022
1 parent 285adcc commit 0f1e373
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
10 changes: 9 additions & 1 deletion suite/suite.go
Expand Up @@ -138,8 +138,10 @@ func Run(t *testing.T, suite TestingSuite) {
suite.SetT(t)
defer failOnPanic(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 +154,12 @@ func Run(t *testing.T, suite TestingSuite) {
}

suite.SetT(parentT)

if r != nil {
t.Errorf("test panicked: %v\n%s", r, debug.Stack())
t.FailNow()
}

}()

if setupTestSuite, ok := suite.(SetupTestSuite); ok {
Expand Down
29 changes: 23 additions & 6 deletions suite/suite_test.go
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 0f1e373

Please sign in to comment.