mirror of https://github.com/stretchr/testify.git
suite: correctly set stats on test panic (#1195)
parent
ba1076d8b3
commit
1b73601ae8
|
@ -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()
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -152,6 +158,7 @@ func Run(t *testing.T, suite TestingSuite) {
|
|||
}
|
||||
|
||||
suite.SetT(parentT)
|
||||
failOnPanic(t, r)
|
||||
}()
|
||||
|
||||
if setupTestSuite, ok := suite.(SetupTestSuite); ok {
|
||||
|
|
|
@ -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["TestSomething"]
|
||||
assert.NotZero(t, testStats.Start)
|
||||
assert.NotZero(t, testStats.End)
|
||||
assert.True(t, testStats.Passed)
|
||||
testStats := suiteWithStats.stats.TestStats
|
||||
|
||||
assert.NotZero(t, testStats["TestSomething"].Start)
|
||||
assert.NotZero(t, testStats["TestSomething"].End)
|
||||
assert.True(t, testStats["TestSomething"].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
|
||||
|
|
Loading…
Reference in New Issue