@@ -62,6 +62,12 @@ func (Tester) Teardown(t *testing.T) {
62
62
TLogger .EndTest (t )
63
63
}
64
64
65
+ // Interface defines Tester's methods for use in this package.
66
+ type Interface interface {
67
+ Setup (* testing.T )
68
+ Teardown (* testing.T )
69
+ }
70
+
65
71
func getTestFunc (t * testing.T , xv reflect.Value , name string ) func (* testing.T ) {
66
72
if m := xv .MethodByName (name ); m .IsValid () {
67
73
if f , ok := m .Interface ().(func (* testing.T )); ok {
@@ -74,9 +80,8 @@ func getTestFunc(t *testing.T, xv reflect.Value, name string) func(*testing.T) {
74
80
}
75
81
76
82
// RunSubTests runs all "Test___" functions that are methods of x as subtests
77
- // of the current test. If x contains methods "Setup(*testing.T)" or
78
- // "Teardown(*testing.T)", those are run before or after each of the test
79
- // functions, respectively.
83
+ // of the current test. Setup is run before the test function and Teardown is
84
+ // run after.
80
85
//
81
86
// For example usage, see example_test.go. Run it using:
82
87
//
@@ -85,13 +90,10 @@ func getTestFunc(t *testing.T, xv reflect.Value, name string) func(*testing.T) {
85
90
// To run a specific test/subtest:
86
91
//
87
92
// $ go test -v -run 'TestExample/^Something$' .
88
- func RunSubTests (t * testing.T , x any ) {
93
+ func RunSubTests (t * testing.T , x Interface ) {
89
94
xt := reflect .TypeOf (x )
90
95
xv := reflect .ValueOf (x )
91
96
92
- setup := getTestFunc (t , xv , "Setup" )
93
- teardown := getTestFunc (t , xv , "Teardown" )
94
-
95
97
for i := 0 ; i < xt .NumMethod (); i ++ {
96
98
methodName := xt .Method (i ).Name
97
99
if ! strings .HasPrefix (methodName , "Test" ) {
@@ -104,8 +106,8 @@ func RunSubTests(t *testing.T, x any) {
104
106
//
105
107
// Note that a defer would run before t.Cleanup, so if a goroutine
106
108
// is closed by a test's t.Cleanup, a deferred leakcheck would fail.
107
- t .Cleanup (func () { teardown (t ) })
108
- setup (t )
109
+ t .Cleanup (func () { x . Teardown (t ) })
110
+ x . Setup (t )
109
111
tfunc (t )
110
112
})
111
113
}
0 commit comments