Skip to content

Commit b50b016

Browse files
authored
suite.Run: simplify running of Setup/TeardownSuite (#1769)
## Summary Improve readability of suite.Run by moving the running of SetupSuite outside of the loop iterating over the (test) methods. This also allows for other simplifications further down in the code. ## Changes - Move SetupSuite to outside the loop - Don't run Setup/TeardownSuite if no tests are found (not new behaviour, but new check) - Remove variable to keep track of wether SetupSuite was executed or not ## Motivation This is a subset of the changes I made under PR #1749. It was suggested by @dolmen to open a separate PR for this part. ## Related issues N/A
1 parent 7c2bbf9 commit b50b016

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

suite/suite.go

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,6 @@ func Run(t *testing.T, suite TestingSuite) {
128128
suite.SetT(t)
129129
suite.SetS(suite)
130130

131-
var suiteSetupDone bool
132-
133131
var stats *SuiteInformation
134132
if _, ok := suite.(WithStats); ok {
135133
stats = newSuiteInformation()
@@ -152,18 +150,6 @@ func Run(t *testing.T, suite TestingSuite) {
152150
continue
153151
}
154152

155-
if !suiteSetupDone {
156-
if stats != nil {
157-
stats.Start = time.Now()
158-
}
159-
160-
if setupAllSuite, ok := suite.(SetupAllSuite); ok {
161-
setupAllSuite.SetupSuite()
162-
}
163-
164-
suiteSetupDone = true
165-
}
166-
167153
test := test{
168154
name: method.Name,
169155
run: func(t *testing.T) {
@@ -208,19 +194,30 @@ func Run(t *testing.T, suite TestingSuite) {
208194
}
209195
tests = append(tests, test)
210196
}
211-
if suiteSetupDone {
212-
defer func() {
213-
if tearDownAllSuite, ok := suite.(TearDownAllSuite); ok {
214-
tearDownAllSuite.TearDownSuite()
215-
}
216-
217-
if suiteWithStats, measureStats := suite.(WithStats); measureStats {
218-
stats.End = time.Now()
219-
suiteWithStats.HandleStats(suiteName, stats)
220-
}
221-
}()
197+
198+
if len(tests) == 0 {
199+
return
200+
}
201+
202+
if stats != nil {
203+
stats.Start = time.Now()
204+
}
205+
206+
if setupAllSuite, ok := suite.(SetupAllSuite); ok {
207+
setupAllSuite.SetupSuite()
222208
}
223209

210+
defer func() {
211+
if tearDownAllSuite, ok := suite.(TearDownAllSuite); ok {
212+
tearDownAllSuite.TearDownSuite()
213+
}
214+
215+
if suiteWithStats, measureStats := suite.(WithStats); measureStats {
216+
stats.End = time.Now()
217+
suiteWithStats.HandleStats(suiteName, stats)
218+
}
219+
}()
220+
224221
runTests(t, tests)
225222
}
226223

0 commit comments

Comments
 (0)