Skip to content

Commit 0ff4bb4

Browse files
authored
Merge pull request #1751 from stretchr/dolmen/suite-fix-use-of-testing-internals
suite: cleanup use of 'testing' internals at runtime
2 parents 186f165 + 82767ae commit 0ff4bb4

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

suite/suite.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/stretchr/testify/require"
1616
)
1717

18-
var allTestsFilter = func(_, _ string) (bool, error) { return true, nil }
1918
var matchMethod = flag.String("testify.m", "", "regular expression to select tests of the testify suite to run")
2019

2120
// Suite is a basic testing suite with methods for storing and
@@ -116,6 +115,11 @@ func (suite *Suite) Run(name string, subtest func()) bool {
116115
})
117116
}
118117

118+
type test = struct {
119+
name string
120+
run func(t *testing.T)
121+
}
122+
119123
// Run takes a testing suite and runs all of the tests attached
120124
// to it.
121125
func Run(t *testing.T, suite TestingSuite) {
@@ -131,7 +135,7 @@ func Run(t *testing.T, suite TestingSuite) {
131135
stats = newSuiteInformation()
132136
}
133137

134-
tests := []testing.InternalTest{}
138+
var tests []test
135139
methodFinder := reflect.TypeOf(suite)
136140
suiteName := methodFinder.Elem().Name()
137141

@@ -160,9 +164,9 @@ func Run(t *testing.T, suite TestingSuite) {
160164
suiteSetupDone = true
161165
}
162166

163-
test := testing.InternalTest{
164-
Name: method.Name,
165-
F: func(t *testing.T) {
167+
test := test{
168+
name: method.Name,
169+
run: func(t *testing.T) {
166170
parentT := suite.T()
167171
suite.SetT(t)
168172
defer recoverAndFailOnPanic(t)
@@ -229,25 +233,13 @@ func methodFilter(name string) (bool, error) {
229233
return regexp.MatchString(*matchMethod, name)
230234
}
231235

232-
func runTests(t testing.TB, tests []testing.InternalTest) {
236+
func runTests(t *testing.T, tests []test) {
233237
if len(tests) == 0 {
234238
t.Log("warning: no tests to run")
235239
return
236240
}
237241

238-
r, ok := t.(runner)
239-
if !ok { // backwards compatibility with Go 1.6 and below
240-
if !testing.RunTests(allTestsFilter, tests) {
241-
t.Fail()
242-
}
243-
return
244-
}
245-
246242
for _, test := range tests {
247-
r.Run(test.Name, test.F)
243+
t.Run(test.name, test.run)
248244
}
249245
}
250-
251-
type runner interface {
252-
Run(name string, f func(t *testing.T)) bool
253-
}

suite/suite_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import (
1616
"github.com/stretchr/testify/require"
1717
)
1818

19+
// allTestsFilter is a yes filter for testing.RunTests
20+
func allTestsFilter(pat, str string) (bool, error) {
21+
return true, nil
22+
}
23+
1924
// SuiteRequireTwice is intended to test the usage of suite.Require in two
2025
// different tests
2126
type SuiteRequireTwice struct{ Suite }

0 commit comments

Comments
 (0)