From 0f1e3737a30ff8d10cd4cfb8e98ead45784b8390 Mon Sep 17 00:00:00 2001 From: nicoche Date: Tue, 31 May 2022 20:40:32 +0200 Subject: [PATCH] suite: correctly set stats on test panic --- suite/suite.go | 10 +++++++++- suite/suite_test.go | 29 +++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/suite/suite.go b/suite/suite.go index 1c402e8df..232a9b012 100644 --- a/suite/suite.go +++ b/suite/suite.go @@ -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) } @@ -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 { diff --git a/suite/suite_test.go b/suite/suite_test.go index 963a25258..446029a43 100644 --- a/suite/suite_test.go +++ b/suite/suite_test.go @@ -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