fixing suite teardown ordering issue

(cherry picked from commit 92c097a5af7b4bbbe3a66c2e7484f68194cd555a)
pull/869/head
Dinesh Kumar 2019-08-02 17:03:07 +05:30 committed by Boyan Soubachov
parent 858f37ff9b
commit 0d3c8ce9f8
2 changed files with 56 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
"reflect"
"regexp"
"runtime/debug"
"sync"
"testing"
"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
// to it.
func Run(t *testing.T, suite TestingSuite) {
testsSync := &sync.WaitGroup{}
suite.SetT(t)
defer failOnPanic(t)
@ -103,6 +105,7 @@ func Run(t *testing.T, suite TestingSuite) {
}
defer func() {
if tearDownAllSuite, ok := suite.(TearDownAllSuite); ok {
testsSync.Wait()
tearDownAllSuite.TearDownSuite()
}
}()
@ -111,6 +114,9 @@ func Run(t *testing.T, suite TestingSuite) {
test := testing.InternalTest{
Name: method.Name,
F: func(t *testing.T) {
defer func() {
testsSync.Done()
}()
parentT := suite.T()
suite.SetT(t)
defer failOnPanic(t)
@ -134,6 +140,7 @@ func Run(t *testing.T, suite TestingSuite) {
},
}
tests = append(tests, test)
testsSync.Add(1)
}
runTests(t, tests)
}

49
suite/suite_order_test.go Normal file
View File

@ -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")
//}