Skip to content

Commit 1333b5d

Browse files
qerdcvVadym Tishchenko
and
Vadym Tishchenko
authored
Add sub-tests to Suite (#1246)
Co-authored-by: Vadym Tishchenko <v.tishchenko@evopay.com.ua>
1 parent b747d7c commit 1333b5d

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

suite/interfaces.go

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import "testing"
77
type TestingSuite interface {
88
T() *testing.T
99
SetT(*testing.T)
10+
SetS(suite TestingSuite)
1011
}
1112

1213
// SetupAllSuite has a SetupSuite method, which will run before the
@@ -51,3 +52,15 @@ type AfterTest interface {
5152
type WithStats interface {
5253
HandleStats(suiteName string, stats *SuiteInformation)
5354
}
55+
56+
// SetupSubTest has a SetupSubTest method, which will run before each
57+
// subtest in the suite.
58+
type SetupSubTest interface {
59+
SetupSubTest()
60+
}
61+
62+
// TearDownSubTest has a TearDownSubTest method, which will run after
63+
// each subtest in the suite have been run.
64+
type TearDownSubTest interface {
65+
TearDownSubTest()
66+
}

suite/suite.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ var matchMethod = flag.String("testify.m", "", "regular expression to select tes
2222
// retrieving the current *testing.T context.
2323
type Suite struct {
2424
*assert.Assertions
25+
2526
mu sync.RWMutex
2627
require *require.Assertions
2728
t *testing.T
29+
30+
// Parent suite to have access to the implemented methods of parent struct
31+
s TestingSuite
2832
}
2933

3034
// T retrieves the current *testing.T context.
@@ -43,6 +47,12 @@ func (suite *Suite) SetT(t *testing.T) {
4347
suite.require = require.New(t)
4448
}
4549

50+
// SetS needs to set the current test suite as parent
51+
// to get access to the parent methods
52+
func (suite *Suite) SetS(s TestingSuite) {
53+
suite.s = s
54+
}
55+
4656
// Require returns a require context for suite.
4757
func (suite *Suite) Require() *require.Assertions {
4858
suite.mu.Lock()
@@ -85,7 +95,18 @@ func failOnPanic(t *testing.T, r interface{}) {
8595
// Provides compatibility with go test pkg -run TestSuite/TestName/SubTestName.
8696
func (suite *Suite) Run(name string, subtest func()) bool {
8797
oldT := suite.T()
88-
defer suite.SetT(oldT)
98+
99+
if setupSubTest, ok := suite.s.(SetupSubTest); ok {
100+
setupSubTest.SetupSubTest()
101+
}
102+
103+
defer func() {
104+
suite.SetT(oldT)
105+
if tearDownSubTest, ok := suite.s.(TearDownSubTest); ok {
106+
tearDownSubTest.TearDownSubTest()
107+
}
108+
}()
109+
89110
return oldT.Run(name, func(t *testing.T) {
90111
suite.SetT(t)
91112
subtest()
@@ -98,6 +119,7 @@ func Run(t *testing.T, suite TestingSuite) {
98119
defer recoverAndFailOnPanic(t)
99120

100121
suite.SetT(t)
122+
suite.SetS(suite)
101123

102124
var suiteSetupDone bool
103125

suite/suite_test.go

+21-8
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,16 @@ type SuiteTester struct {
151151
Suite
152152

153153
// Keep counts of how many times each method is run.
154-
SetupSuiteRunCount int
155-
TearDownSuiteRunCount int
156-
SetupTestRunCount int
157-
TearDownTestRunCount int
158-
TestOneRunCount int
159-
TestTwoRunCount int
160-
TestSubtestRunCount int
161-
NonTestMethodRunCount int
154+
SetupSuiteRunCount int
155+
TearDownSuiteRunCount int
156+
SetupTestRunCount int
157+
TearDownTestRunCount int
158+
TestOneRunCount int
159+
TestTwoRunCount int
160+
TestSubtestRunCount int
161+
NonTestMethodRunCount int
162+
SetupSubTestRunCount int
163+
TearDownSubTestRunCount int
162164

163165
SuiteNameBefore []string
164166
TestNameBefore []string
@@ -255,6 +257,14 @@ func (suite *SuiteTester) TestSubtest() {
255257
}
256258
}
257259

260+
func (suite *SuiteTester) TearDownSubTest() {
261+
suite.TearDownSubTestRunCount++
262+
}
263+
264+
func (suite *SuiteTester) SetupSubTest() {
265+
suite.SetupSubTestRunCount++
266+
}
267+
258268
type SuiteSkipTester struct {
259269
// Include our basic suite logic.
260270
Suite
@@ -336,6 +346,9 @@ func TestRunSuite(t *testing.T) {
336346
assert.Equal(t, suiteTester.TestTwoRunCount, 1)
337347
assert.Equal(t, suiteTester.TestSubtestRunCount, 1)
338348

349+
assert.Equal(t, suiteTester.TearDownSubTestRunCount, 2)
350+
assert.Equal(t, suiteTester.SetupSubTestRunCount, 2)
351+
339352
// Methods that don't match the test method identifier shouldn't
340353
// have been run at all.
341354
assert.Equal(t, suiteTester.NonTestMethodRunCount, 0)

0 commit comments

Comments
 (0)