mirror of https://github.com/stretchr/testify.git
fixing suite teardown ordering issue
(cherry picked from commit 92c097a5af7b4bbbe3a66c2e7484f68194cd555a)pull/869/head
parent
858f37ff9b
commit
0d3c8ce9f8
|
@ -7,6 +7,7 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -80,6 +81,7 @@ func (suite *Suite) Run(name string, subtest func()) bool {
|
||||||
// Run takes a testing suite and runs all of the tests attached
|
// Run takes a testing suite and runs all of the tests attached
|
||||||
// to it.
|
// to it.
|
||||||
func Run(t *testing.T, suite TestingSuite) {
|
func Run(t *testing.T, suite TestingSuite) {
|
||||||
|
testsSync := &sync.WaitGroup{}
|
||||||
suite.SetT(t)
|
suite.SetT(t)
|
||||||
defer failOnPanic(t)
|
defer failOnPanic(t)
|
||||||
|
|
||||||
|
@ -103,6 +105,7 @@ func Run(t *testing.T, suite TestingSuite) {
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
if tearDownAllSuite, ok := suite.(TearDownAllSuite); ok {
|
if tearDownAllSuite, ok := suite.(TearDownAllSuite); ok {
|
||||||
|
testsSync.Wait()
|
||||||
tearDownAllSuite.TearDownSuite()
|
tearDownAllSuite.TearDownSuite()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -111,6 +114,9 @@ func Run(t *testing.T, suite TestingSuite) {
|
||||||
test := testing.InternalTest{
|
test := testing.InternalTest{
|
||||||
Name: method.Name,
|
Name: method.Name,
|
||||||
F: func(t *testing.T) {
|
F: func(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
testsSync.Done()
|
||||||
|
}()
|
||||||
parentT := suite.T()
|
parentT := suite.T()
|
||||||
suite.SetT(t)
|
suite.SetT(t)
|
||||||
defer failOnPanic(t)
|
defer failOnPanic(t)
|
||||||
|
@ -134,6 +140,7 @@ func Run(t *testing.T, suite TestingSuite) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
tests = append(tests, test)
|
tests = append(tests, test)
|
||||||
|
testsSync.Add(1)
|
||||||
}
|
}
|
||||||
runTests(t, tests)
|
runTests(t, tests)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
package suite
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CallOrderSuite struct {
|
||||||
|
Suite
|
||||||
|
callOrder []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *CallOrderSuite) call(method string) {
|
||||||
|
// s.Mutex.Lock()
|
||||||
|
// defer s.Mutex.Unlock()
|
||||||
|
|
||||||
|
s.callOrder = append(s.callOrder, method)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSuiteCallOrder(t *testing.T) {
|
||||||
|
Run(t, new(CallOrderSuite))
|
||||||
|
}
|
||||||
|
func (s *CallOrderSuite) SetupSuite() {
|
||||||
|
s.call("SetupSuite")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *CallOrderSuite) TearDownSuite() {
|
||||||
|
s.call("TearDownSuite")
|
||||||
|
assert.Equal(s.T(), "SetupSuite;SetupTest;Test A;TearDownTest;TearDownSuite", strings.Join(s.callOrder, ";"))
|
||||||
|
}
|
||||||
|
func (s *CallOrderSuite) SetupTest() {
|
||||||
|
s.T().Parallel()
|
||||||
|
s.call("SetupTest")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *CallOrderSuite) TearDownTest() {
|
||||||
|
s.call("TearDownTest")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *CallOrderSuite) Test_A() {
|
||||||
|
s.call("Test A")
|
||||||
|
}
|
||||||
|
|
||||||
|
//func (s *CallOrderSuite) Test_B() {
|
||||||
|
// time.Sleep(time.Second)
|
||||||
|
// s.call("Test B")
|
||||||
|
//}
|
Loading…
Reference in New Issue