Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed suite TearDown ordering #799

Merged
merged 3 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 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,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)
Expand All @@ -134,6 +138,7 @@ func Run(t *testing.T, suite TestingSuite) {
},
}
tests = append(tests, test)
testsSync.Add(1)
}
runTests(t, tests)
}
Expand Down
41 changes: 40 additions & 1 deletion suite/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package suite
import (
"errors"
"io/ioutil"
"math/rand"
"os"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -350,7 +352,7 @@ func TestRunSuite(t *testing.T) {
type SuiteSetupSkipTester struct {
Suite

setUp bool
setUp bool
toreDown bool
}

Expand Down Expand Up @@ -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")
}