Skip to content

Commit

Permalink
suite: fix subtest names (fix #1501) (#1504)
Browse files Browse the repository at this point in the history
* suite: fix TestSubtestPanic failure (#1501)

The subtest of TestSubtestPanic is expected to fail, but that must not
make the testuite of package 'suite' to fail. So call Skip to make 'go
test' to ignore that expected failure.

* suite: fix subtests names

We are doing dirty things with testing.InternalTest. It looks like test
names should have the same prefix as the parent test, like if they were
true subtests (testing.T.Run).

So until we drop our usage of testing.InternamTest, let's rename the
tests.

Also added a few checks of the testing.RunTests result.
  • Loading branch information
dolmen authored Nov 9, 2023
1 parent 331c520 commit db8608e
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions suite/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestSuiteRequireTwice(t *testing.T) {
ok := testing.RunTests(
allTestsFilter,
[]testing.InternalTest{{
Name: "TestSuiteRequireTwice",
Name: t.Name() + "/SuiteRequireTwice",
F: func(t *testing.T) {
suite := new(SuiteRequireTwice)
Run(t, suite)
Expand Down Expand Up @@ -104,31 +104,31 @@ func TestSuiteRecoverPanic(t *testing.T) {
ok := true
panickingTests := []testing.InternalTest{
{
Name: "TestPanicInSetupSuite",
Name: t.Name() + "/InSetupSuite",
F: func(t *testing.T) { Run(t, &panickingSuite{panicInSetupSuite: true}) },
},
{
Name: "TestPanicInSetupTest",
Name: t.Name() + "/InSetupTest",
F: func(t *testing.T) { Run(t, &panickingSuite{panicInSetupTest: true}) },
},
{
Name: "TestPanicInBeforeTest",
Name: t.Name() + "InBeforeTest",
F: func(t *testing.T) { Run(t, &panickingSuite{panicInBeforeTest: true}) },
},
{
Name: "TestPanicInTest",
Name: t.Name() + "/InTest",
F: func(t *testing.T) { Run(t, &panickingSuite{panicInTest: true}) },
},
{
Name: "TestPanicInAfterTest",
Name: t.Name() + "/InAfterTest",
F: func(t *testing.T) { Run(t, &panickingSuite{panicInAfterTest: true}) },
},
{
Name: "TestPanicInTearDownTest",
Name: t.Name() + "/InTearDownTest",
F: func(t *testing.T) { Run(t, &panickingSuite{panicInTearDownTest: true}) },
},
{
Name: "TestPanicInTearDownSuite",
Name: t.Name() + "/InTearDownSuite",
F: func(t *testing.T) { Run(t, &panickingSuite{panicInTearDownSuite: true}) },
},
}
Expand Down Expand Up @@ -451,7 +451,7 @@ func TestSuiteLogging(t *testing.T) {
suiteLoggingTester := new(SuiteLoggingTester)
capture := StdoutCapture{}
internalTest := testing.InternalTest{
Name: "SomeTest",
Name: t.Name() + "/SuiteLoggingTester",
F: func(subT *testing.T) {
Run(subT, suiteLoggingTester)
},
Expand Down Expand Up @@ -552,14 +552,15 @@ func (s *suiteWithStats) TestPanic() {
func TestSuiteWithStats(t *testing.T) {
suiteWithStats := new(suiteWithStats)

testing.RunTests(allTestsFilter, []testing.InternalTest{
suiteSuccess := testing.RunTests(allTestsFilter, []testing.InternalTest{
{
Name: "WithStats",
Name: t.Name() + "/suiteWithStats",
F: func(t *testing.T) {
Run(t, suiteWithStats)
},
},
})
require.False(t, suiteSuccess, "suiteWithStats should report test failure because of panic in TestPanic")

assert.True(t, suiteWithStats.wasCalled)
assert.NotZero(t, suiteWithStats.stats.Start)
Expand Down Expand Up @@ -596,7 +597,7 @@ func TestFailfastSuite(t *testing.T) {
ok := testing.RunTests(
allTestsFilter,
[]testing.InternalTest{{
Name: "TestFailfastSuite",
Name: t.Name() + "/FailfastSuite",
F: func(t *testing.T) {
Run(t, s)
},
Expand Down Expand Up @@ -669,23 +670,24 @@ func (s *subtestPanicSuite) TearDownSubTest() {
}

func (s *subtestPanicSuite) TestSubtestPanic() {
s.Run("subtest", func() {
ok := s.Run("subtest", func() {
panic("panic")
})
s.False(ok, "subtest failure is expected")
}

func TestSubtestPanic(t *testing.T) {
suite := new(subtestPanicSuite)
ok := testing.RunTests(
allTestsFilter,
[]testing.InternalTest{{
Name: "TestSubtestPanic",
Name: t.Name() + "/subtestPanicSuite",
F: func(t *testing.T) {
Run(t, suite)
},
}},
)
assert.False(t, ok)
assert.False(t, ok, "TestSubtestPanic/subtest should make the testsuite fail")
assert.True(t, suite.inTearDownSubTest)
assert.True(t, suite.inTearDownTest)
assert.True(t, suite.inTearDownSuite)
Expand Down

0 comments on commit db8608e

Please sign in to comment.