Skip to content

Commit 98e231d

Browse files
committed
Change implementation to use interface instead of function + PR feedback
1 parent c1b6b49 commit 98e231d

File tree

3 files changed

+116
-40
lines changed

3 files changed

+116
-40
lines changed

suite/interfaces.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,9 @@ type SetupSubTest interface {
6464
type TearDownSubTest interface {
6565
TearDownSubTest()
6666
}
67+
68+
// SkipTest has a SkipTest method, which will run for every test during the
69+
// collection phase.
70+
type SkipTest interface {
71+
SkipTest(testSuiteName, testName string) bool
72+
}

suite/suite.go

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,11 @@ func (suite *Suite) Run(name string, subtest func()) bool {
119119
// Run takes a testing suite and runs all of the tests attached
120120
// to it.
121121
func Run(t *testing.T, suite TestingSuite) {
122-
RunWithSkip(t, suite, func(_, _ string) bool { return false })
123-
}
124-
125-
// RunWithSkip takes a testing suite and a skip function allowing
126-
// for extra filtering on tests to be run
127-
func RunWithSkip(t *testing.T, suite TestingSuite, skip func(string, string) bool) {
128122
defer recoverAndFailOnPanic(t)
129123

130124
suite.SetT(t)
131125
suite.SetS(suite)
132126

133-
var suiteSetupDone bool
134-
135127
var stats *SuiteInformation
136128
if _, ok := suite.(WithStats); ok {
137129
stats = newSuiteInformation()
@@ -154,8 +146,10 @@ func RunWithSkip(t *testing.T, suite TestingSuite, skip func(string, string) boo
154146
continue
155147
}
156148

157-
if skip(suiteName, method.Name) {
158-
continue
149+
if skipTest, ok := suite.(SkipTest); ok {
150+
if skipTest.SkipTest(suiteName, method.Name) {
151+
continue
152+
}
159153
}
160154

161155
test := testing.InternalTest{
@@ -203,30 +197,28 @@ func RunWithSkip(t *testing.T, suite TestingSuite, skip func(string, string) boo
203197
tests = append(tests, test)
204198
}
205199

206-
if len(tests) > 0 {
207-
if stats != nil {
208-
stats.Start = time.Now()
209-
}
200+
if len(tests) == 0 {
201+
return
202+
}
210203

211-
if setupAllSuite, ok := suite.(SetupAllSuite); ok {
212-
setupAllSuite.SetupSuite()
213-
}
204+
if stats != nil {
205+
stats.Start = time.Now()
206+
}
214207

215-
suiteSetupDone = true
208+
if setupAllSuite, ok := suite.(SetupAllSuite); ok {
209+
setupAllSuite.SetupSuite()
216210
}
217211

218-
if suiteSetupDone {
219-
defer func() {
220-
if tearDownAllSuite, ok := suite.(TearDownAllSuite); ok {
221-
tearDownAllSuite.TearDownSuite()
222-
}
212+
defer func() {
213+
if tearDownAllSuite, ok := suite.(TearDownAllSuite); ok {
214+
tearDownAllSuite.TearDownSuite()
215+
}
223216

224-
if suiteWithStats, measureStats := suite.(WithStats); measureStats {
225-
stats.End = time.Now()
226-
suiteWithStats.HandleStats(suiteName, stats)
227-
}
228-
}()
229-
}
217+
if suiteWithStats, measureStats := suite.(WithStats); measureStats {
218+
stats.End = time.Now()
219+
suiteWithStats.HandleStats(suiteName, stats)
220+
}
221+
}()
230222

231223
runTests(t, tests)
232224
}

suite/suite_test.go

Lines changed: 89 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"errors"
66
"flag"
7+
"fmt"
78
"io"
89
"math/rand"
910
"os"
@@ -747,17 +748,83 @@ func TestUnInitializedSuites(t *testing.T) {
747748
})
748749
}
749750

750-
var register = map[string]bool{"SuiteTesterTestTwo": true}
751+
// Test/Example of SkipTest
751752

752-
func skip(suiteName, testName string) bool {
753-
return register[suiteName+testName]
753+
type buildVersion struct {
754+
major int
755+
minor int
756+
}
757+
758+
func (v buildVersion) after(other buildVersion) bool {
759+
if v.major > other.major {
760+
return true
761+
}
762+
if v.major < other.major {
763+
return false
764+
}
765+
if v.minor > other.minor {
766+
return true
767+
}
768+
return false
769+
}
770+
771+
type testRegistration struct {
772+
release buildVersion
773+
features []string
774+
}
775+
776+
var testRegistrations = map[string]testRegistration{
777+
"SuiteWithSkipTestOne": {buildVersion{1, 2}, []string{"myOldFeature"}},
778+
"SuiteWithSkipTestTwo": {buildVersion{2, 0}, []string{"myNewFeature"}},
779+
}
780+
781+
func contains(s []string, v string) bool {
782+
for i := range s {
783+
if v == s[i] {
784+
return true
785+
}
786+
}
787+
return false
788+
}
789+
790+
// reusing the Suite struct defined above
791+
type SuiteWithSkip struct {
792+
SuiteTester
793+
}
794+
795+
// SkipTest Implements the SkipTest interface
796+
func (s *SuiteWithSkip) SkipTest(testSuiteName string, testName string) bool {
797+
testRegister, ok := testRegistrations[testSuiteName+testName]
798+
if !ok {
799+
return false
800+
}
801+
802+
// runParameters.
803+
// These could for example be defined in a file, or as cli arguments.
804+
// In this example we define them here
805+
currentVersion := buildVersion{1, 2}
806+
enabledFeatures := []string{"myOldFeature", "myNewFeature"}
807+
808+
if testRegister.release.after(currentVersion) {
809+
fmt.Printf("Skipping %s, due to release", testName)
810+
return true
811+
}
812+
813+
for _, registeredFeature := range testRegister.features {
814+
if !contains(enabledFeatures, registeredFeature) {
815+
fmt.Printf("Skipping %s, due to feature", testName)
816+
return true
817+
}
818+
}
819+
820+
return false
754821
}
755822

756823
// TestRunSuiteWithSkip will be run by the 'go test' command, so within it, we
757824
// can run our suite using the Run(*testing.T, TestingSuite) function.
758825
func TestRunSuiteWithSkip(t *testing.T) {
759-
suiteTester := new(SuiteTester)
760-
RunWithSkip(t, suiteTester, skip)
826+
suiteTester := new(SuiteWithSkip)
827+
Run(t, suiteTester)
761828

762829
// Normally, the test would end here. The following are simply
763830
// some assertions to ensure that the Run function is working as
@@ -790,11 +857,11 @@ func TestRunSuiteWithSkip(t *testing.T) {
790857
assert.Contains(t, suiteTester.TearDownSubTestNames, "TestRunSuiteWithSkip/TestSubtest/second")
791858

792859
for _, suiteName := range suiteTester.SuiteNameAfter {
793-
assert.Equal(t, "SuiteTester", suiteName)
860+
assert.Equal(t, "SuiteWithSkip", suiteName)
794861
}
795862

796863
for _, suiteName := range suiteTester.SuiteNameBefore {
797-
assert.Equal(t, "SuiteTester", suiteName)
864+
assert.Equal(t, "SuiteWithSkip", suiteName)
798865
}
799866

800867
for _, when := range suiteTester.TimeAfter {
@@ -834,11 +901,22 @@ func TestRunSuiteWithSkip(t *testing.T) {
834901

835902
}
836903

904+
// SuiteWithSkipAll reuses the Suite struct defined above and implements the SkipTest interface skipping all tests
905+
type SuiteWithSkipAll struct {
906+
SuiteTester
907+
}
908+
909+
// SkipTest Implements the SkipTest interface all tests and Setup/TeardownSuite will be skipped
910+
func (s *SuiteWithSkipAll) SkipTest(testSuiteName string, testName string) bool {
911+
return true
912+
}
913+
837914
// TestRunSuiteWithSkip will be run by the 'go test' command, so within it, we
838915
// can run our suite using the Run(*testing.T, TestingSuite) function.
916+
839917
func TestRunSuiteWithSkipAll(t *testing.T) {
840-
suiteTester := new(SuiteTester)
841-
RunWithSkip(t, suiteTester, func(_, _ string) bool { return true })
918+
suiteTester := new(SuiteWithSkipAll)
919+
Run(t, suiteTester)
842920

843921
// Normally, the test would end here. The following are simply
844922
// some assertions to ensure that the Run function is working as
@@ -871,11 +949,11 @@ func TestRunSuiteWithSkipAll(t *testing.T) {
871949
assert.NotContains(t, suiteTester.TearDownSubTestNames, "TestRunSuiteWithSkipAll/TestSubtest/second")
872950

873951
for _, suiteName := range suiteTester.SuiteNameAfter {
874-
assert.Equal(t, "SuiteTester", suiteName)
952+
assert.Equal(t, "SuiteWithSkipAll", suiteName)
875953
}
876954

877955
for _, suiteName := range suiteTester.SuiteNameBefore {
878-
assert.Equal(t, "SuiteTester", suiteName)
956+
assert.Equal(t, "SuiteWithSkipAll", suiteName)
879957
}
880958

881959
for _, when := range suiteTester.TimeAfter {

0 commit comments

Comments
 (0)