Skip to content

Commit 10cdced

Browse files
committed
Change implementation to use interface instead of function + PR feedback
1 parent 55fbc86 commit 10cdced

File tree

3 files changed

+99
-19
lines changed

3 files changed

+99
-19
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: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,6 @@ type test = struct {
123123
// Run takes a testing suite and runs all of the tests attached
124124
// to it.
125125
func Run(t *testing.T, suite TestingSuite) {
126-
RunWithSkip(t, suite, func(_, _ string) bool { return false })
127-
}
128-
129-
// RunWithSkip takes a testing suite and a skip function allowing
130-
// for extra filtering on tests to be run
131-
func RunWithSkip(t *testing.T, suite TestingSuite, skip func(string, string) bool) {
132126
defer recoverAndFailOnPanic(t)
133127

134128
suite.SetT(t)
@@ -156,8 +150,10 @@ func RunWithSkip(t *testing.T, suite TestingSuite, skip func(string, string) boo
156150
continue
157151
}
158152

159-
if skip(suiteName, method.Name) {
160-
continue
153+
if skipTest, ok := suite.(SkipTest); ok {
154+
if skipTest.SkipTest(suiteName, method.Name) {
155+
continue
156+
}
161157
}
162158

163159
test := test{

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"
@@ -752,17 +753,83 @@ func TestUnInitializedSuites(t *testing.T) {
752753
})
753754
}
754755

755-
var register = map[string]bool{"SuiteTesterTestTwo": true}
756+
// Test/Example of SkipTest
756757

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

761828
// TestRunSuiteWithSkip will be run by the 'go test' command, so within it, we
762829
// can run our suite using the Run(*testing.T, TestingSuite) function.
763830
func TestRunSuiteWithSkip(t *testing.T) {
764-
suiteTester := new(SuiteTester)
765-
RunWithSkip(t, suiteTester, skip)
831+
suiteTester := new(SuiteWithSkip)
832+
Run(t, suiteTester)
766833

767834
// Normally, the test would end here. The following are simply
768835
// some assertions to ensure that the Run function is working as
@@ -795,11 +862,11 @@ func TestRunSuiteWithSkip(t *testing.T) {
795862
assert.Contains(t, suiteTester.TearDownSubTestNames, "TestRunSuiteWithSkip/TestSubtest/second")
796863

797864
for _, suiteName := range suiteTester.SuiteNameAfter {
798-
assert.Equal(t, "SuiteTester", suiteName)
865+
assert.Equal(t, "SuiteWithSkip", suiteName)
799866
}
800867

801868
for _, suiteName := range suiteTester.SuiteNameBefore {
802-
assert.Equal(t, "SuiteTester", suiteName)
869+
assert.Equal(t, "SuiteWithSkip", suiteName)
803870
}
804871

805872
for _, when := range suiteTester.TimeAfter {
@@ -839,11 +906,22 @@ func TestRunSuiteWithSkip(t *testing.T) {
839906

840907
}
841908

909+
// SuiteWithSkipAll reuses the Suite struct defined above and implements the SkipTest interface skipping all tests
910+
type SuiteWithSkipAll struct {
911+
SuiteTester
912+
}
913+
914+
// SkipTest Implements the SkipTest interface all tests and Setup/TeardownSuite will be skipped
915+
func (s *SuiteWithSkipAll) SkipTest(testSuiteName string, testName string) bool {
916+
return true
917+
}
918+
842919
// TestRunSuiteWithSkip will be run by the 'go test' command, so within it, we
843920
// can run our suite using the Run(*testing.T, TestingSuite) function.
921+
844922
func TestRunSuiteWithSkipAll(t *testing.T) {
845-
suiteTester := new(SuiteTester)
846-
RunWithSkip(t, suiteTester, func(_, _ string) bool { return true })
923+
suiteTester := new(SuiteWithSkipAll)
924+
Run(t, suiteTester)
847925

848926
// Normally, the test would end here. The following are simply
849927
// some assertions to ensure that the Run function is working as
@@ -876,11 +954,11 @@ func TestRunSuiteWithSkipAll(t *testing.T) {
876954
assert.NotContains(t, suiteTester.TearDownSubTestNames, "TestRunSuiteWithSkipAll/TestSubtest/second")
877955

878956
for _, suiteName := range suiteTester.SuiteNameAfter {
879-
assert.Equal(t, "SuiteTester", suiteName)
957+
assert.Equal(t, "SuiteWithSkipAll", suiteName)
880958
}
881959

882960
for _, suiteName := range suiteTester.SuiteNameBefore {
883-
assert.Equal(t, "SuiteTester", suiteName)
961+
assert.Equal(t, "SuiteWithSkipAll", suiteName)
884962
}
885963

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

0 commit comments

Comments
 (0)