Skip to content

Commit

Permalink
WIP: fixing suite teardown ordering issue
Browse files Browse the repository at this point in the history
(cherry picked from commit 92c097a5af7b4bbbe3a66c2e7484f68194cd555a)
  • Loading branch information
Dinesh Kumar committed Aug 2, 2019
1 parent 221dbe5 commit d0039c3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
9 changes: 8 additions & 1 deletion suite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"reflect"
"regexp"
"runtime/debug"
"sync"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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++ {
Expand All @@ -103,6 +105,7 @@ func Run(t *testing.T, suite TestingSuite) {
}
defer func() {
if tearDownAllSuite, ok := suite.(TearDownAllSuite); ok {
testsSync.Wait()
tearDownAllSuite.TearDownSuite()
}
}()
Expand All @@ -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)
Expand All @@ -134,6 +140,7 @@ func Run(t *testing.T, suite TestingSuite) {
},
}
tests = append(tests, test)
testsSync.Add(1)
}
runTests(t, tests)
}
Expand Down
49 changes: 49 additions & 0 deletions suite/suite_order_test.go
Original file line number Diff line number Diff line change
@@ -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")
//}

0 comments on commit d0039c3

Please sign in to comment.