mirror of
https://github.com/stretchr/testify.git
synced 2025-05-31 11:42:44 +00:00
suite: cleanup use of 'testing' internals at runtime
Cleanup runtime use of stdlib's testing internals which was required for older Go versions. Note: we are still using testing.RunTests in the suite's test suite for now.
This commit is contained in:
parent
186f1654cc
commit
82767aed18
@ -15,7 +15,6 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
var allTestsFilter = func(_, _ string) (bool, error) { return true, nil }
|
|
||||||
var matchMethod = flag.String("testify.m", "", "regular expression to select tests of the testify suite to run")
|
var matchMethod = flag.String("testify.m", "", "regular expression to select tests of the testify suite to run")
|
||||||
|
|
||||||
// Suite is a basic testing suite with methods for storing and
|
// Suite is a basic testing suite with methods for storing and
|
||||||
@ -116,6 +115,11 @@ func (suite *Suite) Run(name string, subtest func()) bool {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type test = struct {
|
||||||
|
name string
|
||||||
|
run func(t *testing.T)
|
||||||
|
}
|
||||||
|
|
||||||
// Run takes a testing suite and runs all of the tests attached
|
// Run takes a testing suite and runs all of the tests attached
|
||||||
// to it.
|
// to it.
|
||||||
func Run(t *testing.T, suite TestingSuite) {
|
func Run(t *testing.T, suite TestingSuite) {
|
||||||
@ -131,7 +135,7 @@ func Run(t *testing.T, suite TestingSuite) {
|
|||||||
stats = newSuiteInformation()
|
stats = newSuiteInformation()
|
||||||
}
|
}
|
||||||
|
|
||||||
tests := []testing.InternalTest{}
|
var tests []test
|
||||||
methodFinder := reflect.TypeOf(suite)
|
methodFinder := reflect.TypeOf(suite)
|
||||||
suiteName := methodFinder.Elem().Name()
|
suiteName := methodFinder.Elem().Name()
|
||||||
|
|
||||||
@ -160,9 +164,9 @@ func Run(t *testing.T, suite TestingSuite) {
|
|||||||
suiteSetupDone = true
|
suiteSetupDone = true
|
||||||
}
|
}
|
||||||
|
|
||||||
test := testing.InternalTest{
|
test := test{
|
||||||
Name: method.Name,
|
name: method.Name,
|
||||||
F: func(t *testing.T) {
|
run: func(t *testing.T) {
|
||||||
parentT := suite.T()
|
parentT := suite.T()
|
||||||
suite.SetT(t)
|
suite.SetT(t)
|
||||||
defer recoverAndFailOnPanic(t)
|
defer recoverAndFailOnPanic(t)
|
||||||
@ -229,25 +233,13 @@ func methodFilter(name string) (bool, error) {
|
|||||||
return regexp.MatchString(*matchMethod, name)
|
return regexp.MatchString(*matchMethod, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runTests(t testing.TB, tests []testing.InternalTest) {
|
func runTests(t *testing.T, tests []test) {
|
||||||
if len(tests) == 0 {
|
if len(tests) == 0 {
|
||||||
t.Log("warning: no tests to run")
|
t.Log("warning: no tests to run")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r, ok := t.(runner)
|
|
||||||
if !ok { // backwards compatibility with Go 1.6 and below
|
|
||||||
if !testing.RunTests(allTestsFilter, tests) {
|
|
||||||
t.Fail()
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
r.Run(test.Name, test.F)
|
t.Run(test.name, test.run)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type runner interface {
|
|
||||||
Run(name string, f func(t *testing.T)) bool
|
|
||||||
}
|
|
||||||
|
@ -16,6 +16,11 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// allTestsFilter is a yes filter for testing.RunTests
|
||||||
|
func allTestsFilter(pat, str string) (bool, error) {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
// SuiteRequireTwice is intended to test the usage of suite.Require in two
|
// SuiteRequireTwice is intended to test the usage of suite.Require in two
|
||||||
// different tests
|
// different tests
|
||||||
type SuiteRequireTwice struct{ Suite }
|
type SuiteRequireTwice struct{ Suite }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user