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

Ensure that subtest name is set in SetupSubTest/TearDownSubTest #1393

Closed
wants to merge 1 commit into from
Closed
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
21 changes: 11 additions & 10 deletions suite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,20 @@ func failOnPanic(t *testing.T, r interface{}) {
func (suite *Suite) Run(name string, subtest func()) bool {
oldT := suite.T()

if setupSubTest, ok := suite.s.(SetupSubTest); ok {
setupSubTest.SetupSubTest()
}
return oldT.Run(name, func(t *testing.T) {
suite.SetT(t)

defer func() {
suite.SetT(oldT)
if tearDownSubTest, ok := suite.s.(TearDownSubTest); ok {
tearDownSubTest.TearDownSubTest()
if setupSubTest, ok := suite.s.(SetupSubTest); ok {
setupSubTest.SetupSubTest()
}
}()

return oldT.Run(name, func(t *testing.T) {
suite.SetT(t)
defer func() {
if tearDownSubTest, ok := suite.s.(TearDownSubTest); ok {
tearDownSubTest.TearDownSubTest()
}
suite.SetT(oldT)
}()

subtest()
})
}
Expand Down
19 changes: 16 additions & 3 deletions suite/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ type SuiteTester struct {
SetupSubTestRunCount int
TearDownSubTestRunCount int

SetupSubTestNames []string
TearDownSubTestNames []string

SuiteNameBefore []string
TestNameBefore []string

Expand All @@ -170,6 +173,8 @@ type SuiteTester struct {

TimeBefore []time.Time
TimeAfter []time.Time

SuiteT *testing.T
}

// The SetupSuite method will be run by testify once, at the very
Expand Down Expand Up @@ -245,23 +250,25 @@ func (suite *SuiteTester) TestSubtest() {
{"first"},
{"second"},
} {
suiteT := suite.T()
suite.SuiteT = suite.T()
suite.Run(t.testName, func() {
// We should get a different *testing.T for subtests, so that
// go test recognizes them as proper subtests for output formatting
// and running individual subtests
subTestT := suite.T()
suite.NotEqual(subTestT, suiteT)
suite.NotEqual(subTestT, suite.SuiteT)
})
suite.Equal(suiteT, suite.T())
suite.Equal(suite.SuiteT, suite.T())
}
}

func (suite *SuiteTester) TearDownSubTest() {
suite.TearDownSubTestNames = append(suite.TearDownSubTestNames, suite.T().Name())
suite.TearDownSubTestRunCount++
}

func (suite *SuiteTester) SetupSubTest() {
suite.SetupSubTestNames = append(suite.SetupSubTestNames, suite.T().Name())
suite.SetupSubTestRunCount++
}

Expand Down Expand Up @@ -319,6 +326,12 @@ func TestRunSuite(t *testing.T) {
assert.Contains(t, suiteTester.TestNameBefore, "TestSkip")
assert.Contains(t, suiteTester.TestNameBefore, "TestSubtest")

assert.Contains(t, suiteTester.SetupSubTestNames, "TestRunSuite/TestSubtest/first")
assert.Contains(t, suiteTester.SetupSubTestNames, "TestRunSuite/TestSubtest/second")

assert.Contains(t, suiteTester.TearDownSubTestNames, "TestRunSuite/TestSubtest/first")
assert.Contains(t, suiteTester.TearDownSubTestNames, "TestRunSuite/TestSubtest/second")

for _, suiteName := range suiteTester.SuiteNameAfter {
assert.Equal(t, "SuiteTester", suiteName)
}
Expand Down
Loading