diff --git a/suite/suite.go b/suite/suite.go index d708d7d75..619530187 100644 --- a/suite/suite.go +++ b/suite/suite.go @@ -7,6 +7,7 @@ import ( "reflect" "regexp" "runtime/debug" + "sync" "testing" "github.com/stretchr/testify/assert" @@ -80,11 +81,12 @@ 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) suiteSetupDone := false - + methodFinder := reflect.TypeOf(suite) tests := []testing.InternalTest{} for index := 0; index < methodFinder.NumMethod(); index++ { @@ -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,7 @@ func Run(t *testing.T, suite TestingSuite) { test := testing.InternalTest{ Name: method.Name, F: func(t *testing.T) { + defer testsSync.Done() parentT := suite.T() suite.SetT(t) defer failOnPanic(t) @@ -134,6 +138,7 @@ func Run(t *testing.T, suite TestingSuite) { }, } tests = append(tests, test) + testsSync.Add(1) } runTests(t, tests) } diff --git a/suite/suite_test.go b/suite/suite_test.go index 26dddbd36..5d757dd1b 100644 --- a/suite/suite_test.go +++ b/suite/suite_test.go @@ -3,7 +3,9 @@ package suite import ( "errors" "io/ioutil" + "math/rand" "os" + "strings" "testing" "time" @@ -350,7 +352,7 @@ func TestRunSuite(t *testing.T) { type SuiteSetupSkipTester struct { Suite - setUp bool + setUp bool toreDown bool } @@ -443,3 +445,40 @@ func TestSuiteLogging(t *testing.T) { assert.NotContains(t, output, "TESTLOGPASS") } } + +type CallOrderSuite struct { + Suite + callOrder []string +} + +func (s *CallOrderSuite) call(method string) { + time.Sleep(time.Duration(rand.Intn(300)) * time.Millisecond) + 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;SetupTest;Test B;TearDownTest;TearDownSuite", strings.Join(s.callOrder, ";")) +} +func (s *CallOrderSuite) SetupTest() { + s.call("SetupTest") +} + +func (s *CallOrderSuite) TearDownTest() { + s.call("TearDownTest") +} + +func (s *CallOrderSuite) Test_A() { + s.call("Test A") +} + +func (s *CallOrderSuite) Test_B() { + s.call("Test B") +}