From 3104bf5483d3fba84f24933949f0533f4467880b Mon Sep 17 00:00:00 2001 From: Ben Blount Date: Wed, 18 Jan 2017 17:57:21 -0800 Subject: [PATCH] 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 #346 --- suite/suite.go | 21 ++++++++++++++++++--- suite/suite_test.go | 18 +++++++++++------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/suite/suite.go b/suite/suite.go index 991d3be80..e20afbc21 100644 --- a/suite/suite.go +++ b/suite/suite.go @@ -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) +} + +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 + } - if !testing.RunTests(func(_, _ string) (bool, error) { return true, nil }, - tests) { - t.Fail() + 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 +} diff --git a/suite/suite_test.go b/suite/suite_test.go index 641923480..b75fa4ac1 100644 --- a/suite/suite_test.go +++ b/suite/suite_test.go @@ -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")