mirror of https://github.com/stretchr/testify.git
Use Go 1.7 subtests so suites can properly nest
This removes dependencies on Go Testing Internals and also enables test methods within a suite to have subtests. Fixes #346pull/356/head
parent
b8c9b4ef3d
commit
3104bf5483
|
@ -12,6 +12,7 @@ import (
|
|||
"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")
|
||||
|
||||
// Suite is a basic testing suite with methods for storing and
|
||||
|
@ -104,10 +105,20 @@ func Run(t *testing.T, suite TestingSuite) {
|
|||
tests = append(tests, test)
|
||||
}
|
||||
}
|
||||
runTests(t, tests)
|
||||
}
|
||||
|
||||
if !testing.RunTests(func(_, _ string) (bool, error) { return true, nil },
|
||||
tests) {
|
||||
t.Fail()
|
||||
func runTests(t testing.TB, tests []testing.InternalTest) {
|
||||
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 {
|
||||
r.Run(test.Name, test.F)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -119,3 +130,7 @@ func methodFilter(name string) (bool, error) {
|
|||
}
|
||||
return regexp.MatchString(*matchMethod, name)
|
||||
}
|
||||
|
||||
type runner interface {
|
||||
Run(name string, f func(t *testing.T)) bool
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// SuiteRequireTwice is intended to test the usage of suite.Require in two
|
||||
|
@ -19,7 +20,7 @@ type SuiteRequireTwice struct{ Suite }
|
|||
// A regression would result on these tests panicking rather than failing.
|
||||
func TestSuiteRequireTwice(t *testing.T) {
|
||||
ok := testing.RunTests(
|
||||
func(_, _ string) (bool, error) { return true, nil },
|
||||
allTestsFilter,
|
||||
[]testing.InternalTest{{
|
||||
Name: "TestSuiteRequireTwice",
|
||||
F: func(t *testing.T) {
|
||||
|
@ -267,16 +268,19 @@ func (sc *StdoutCapture) StopCapture() (string, error) {
|
|||
}
|
||||
|
||||
func TestSuiteLogging(t *testing.T) {
|
||||
testT := testing.T{}
|
||||
|
||||
suiteLoggingTester := new(SuiteLoggingTester)
|
||||
|
||||
capture := StdoutCapture{}
|
||||
internalTest := testing.InternalTest{
|
||||
Name: "SomeTest",
|
||||
F: func(subT *testing.T) {
|
||||
Run(subT, suiteLoggingTester)
|
||||
},
|
||||
}
|
||||
capture.StartCapture()
|
||||
Run(&testT, suiteLoggingTester)
|
||||
testing.RunTests(allTestsFilter, []testing.InternalTest{internalTest})
|
||||
output, err := capture.StopCapture()
|
||||
|
||||
assert.Nil(t, err, "Got an error trying to capture stdout!")
|
||||
require.NoError(t, err, "Got an error trying to capture stdout and stderr!")
|
||||
require.NotEmpty(t, output, "output content must not be empty")
|
||||
|
||||
// Failed tests' output is always printed
|
||||
assert.Contains(t, output, "TESTLOGFAIL")
|
||||
|
|
Loading…
Reference in New Issue