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 2 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
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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not just defer testSync.Done() instead of wrapping it in an anonymous function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had some logic which i've removed later. good catch, have updated.

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
47 changes: 47 additions & 0 deletions suite/suite_order_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package suite
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is your logic for putting this in a separate file? Does it not make sense to just have this as another test in suite_test.go?

Copy link
Contributor Author

@devdinu devdinu Jan 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other file is huge now, thought could separate the tests based on what we test sortof integration vs small function unit tests.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I do agree that it's becoming a bit large but I don't think navigating it is still a huge problem.

I'd be happy to discuss any ideas you might have about improving its readability & maintainability, whether it involves splitting it up or some other way.


import (
"math/rand"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

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