@@ -62,6 +62,12 @@ func (Tester) Teardown(t *testing.T) {
6262 TLogger .EndTest (t )
6363}
6464
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+
6571func getTestFunc (t * testing.T , xv reflect.Value , name string ) func (* testing.T ) {
6672 if m := xv .MethodByName (name ); m .IsValid () {
6773 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) {
7480}
7581
7682// 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.
8085//
8186// For example usage, see example_test.go. Run it using:
8287//
@@ -85,13 +90,10 @@ func getTestFunc(t *testing.T, xv reflect.Value, name string) func(*testing.T) {
8590// To run a specific test/subtest:
8691//
8792// $ go test -v -run 'TestExample/^Something$' .
88- func RunSubTests (t * testing.T , x any ) {
93+ func RunSubTests (t * testing.T , x Interface ) {
8994 xt := reflect .TypeOf (x )
9095 xv := reflect .ValueOf (x )
9196
92- setup := getTestFunc (t , xv , "Setup" )
93- teardown := getTestFunc (t , xv , "Teardown" )
94-
9597 for i := 0 ; i < xt .NumMethod (); i ++ {
9698 methodName := xt .Method (i ).Name
9799 if ! strings .HasPrefix (methodName , "Test" ) {
@@ -104,8 +106,8 @@ func RunSubTests(t *testing.T, x any) {
104106 //
105107 // Note that a defer would run before t.Cleanup, so if a goroutine
106108 // 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 )
109111 tfunc (t )
110112 })
111113 }
0 commit comments