Skip to content

Commit db8608e

Browse files
authored
suite: fix subtest names (fix #1501) (#1504)
* 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.
1 parent 331c520 commit db8608e

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

suite/suite_test.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestSuiteRequireTwice(t *testing.T) {
2727
ok := testing.RunTests(
2828
allTestsFilter,
2929
[]testing.InternalTest{{
30-
Name: "TestSuiteRequireTwice",
30+
Name: t.Name() + "/SuiteRequireTwice",
3131
F: func(t *testing.T) {
3232
suite := new(SuiteRequireTwice)
3333
Run(t, suite)
@@ -104,31 +104,31 @@ func TestSuiteRecoverPanic(t *testing.T) {
104104
ok := true
105105
panickingTests := []testing.InternalTest{
106106
{
107-
Name: "TestPanicInSetupSuite",
107+
Name: t.Name() + "/InSetupSuite",
108108
F: func(t *testing.T) { Run(t, &panickingSuite{panicInSetupSuite: true}) },
109109
},
110110
{
111-
Name: "TestPanicInSetupTest",
111+
Name: t.Name() + "/InSetupTest",
112112
F: func(t *testing.T) { Run(t, &panickingSuite{panicInSetupTest: true}) },
113113
},
114114
{
115-
Name: "TestPanicInBeforeTest",
115+
Name: t.Name() + "InBeforeTest",
116116
F: func(t *testing.T) { Run(t, &panickingSuite{panicInBeforeTest: true}) },
117117
},
118118
{
119-
Name: "TestPanicInTest",
119+
Name: t.Name() + "/InTest",
120120
F: func(t *testing.T) { Run(t, &panickingSuite{panicInTest: true}) },
121121
},
122122
{
123-
Name: "TestPanicInAfterTest",
123+
Name: t.Name() + "/InAfterTest",
124124
F: func(t *testing.T) { Run(t, &panickingSuite{panicInAfterTest: true}) },
125125
},
126126
{
127-
Name: "TestPanicInTearDownTest",
127+
Name: t.Name() + "/InTearDownTest",
128128
F: func(t *testing.T) { Run(t, &panickingSuite{panicInTearDownTest: true}) },
129129
},
130130
{
131-
Name: "TestPanicInTearDownSuite",
131+
Name: t.Name() + "/InTearDownSuite",
132132
F: func(t *testing.T) { Run(t, &panickingSuite{panicInTearDownSuite: true}) },
133133
},
134134
}
@@ -451,7 +451,7 @@ func TestSuiteLogging(t *testing.T) {
451451
suiteLoggingTester := new(SuiteLoggingTester)
452452
capture := StdoutCapture{}
453453
internalTest := testing.InternalTest{
454-
Name: "SomeTest",
454+
Name: t.Name() + "/SuiteLoggingTester",
455455
F: func(subT *testing.T) {
456456
Run(subT, suiteLoggingTester)
457457
},
@@ -552,14 +552,15 @@ func (s *suiteWithStats) TestPanic() {
552552
func TestSuiteWithStats(t *testing.T) {
553553
suiteWithStats := new(suiteWithStats)
554554

555-
testing.RunTests(allTestsFilter, []testing.InternalTest{
555+
suiteSuccess := testing.RunTests(allTestsFilter, []testing.InternalTest{
556556
{
557-
Name: "WithStats",
557+
Name: t.Name() + "/suiteWithStats",
558558
F: func(t *testing.T) {
559559
Run(t, suiteWithStats)
560560
},
561561
},
562562
})
563+
require.False(t, suiteSuccess, "suiteWithStats should report test failure because of panic in TestPanic")
563564

564565
assert.True(t, suiteWithStats.wasCalled)
565566
assert.NotZero(t, suiteWithStats.stats.Start)
@@ -596,7 +597,7 @@ func TestFailfastSuite(t *testing.T) {
596597
ok := testing.RunTests(
597598
allTestsFilter,
598599
[]testing.InternalTest{{
599-
Name: "TestFailfastSuite",
600+
Name: t.Name() + "/FailfastSuite",
600601
F: func(t *testing.T) {
601602
Run(t, s)
602603
},
@@ -669,23 +670,24 @@ func (s *subtestPanicSuite) TearDownSubTest() {
669670
}
670671

671672
func (s *subtestPanicSuite) TestSubtestPanic() {
672-
s.Run("subtest", func() {
673+
ok := s.Run("subtest", func() {
673674
panic("panic")
674675
})
676+
s.False(ok, "subtest failure is expected")
675677
}
676678

677679
func TestSubtestPanic(t *testing.T) {
678680
suite := new(subtestPanicSuite)
679681
ok := testing.RunTests(
680682
allTestsFilter,
681683
[]testing.InternalTest{{
682-
Name: "TestSubtestPanic",
684+
Name: t.Name() + "/subtestPanicSuite",
683685
F: func(t *testing.T) {
684686
Run(t, suite)
685687
},
686688
}},
687689
)
688-
assert.False(t, ok)
690+
assert.False(t, ok, "TestSubtestPanic/subtest should make the testsuite fail")
689691
assert.True(t, suite.inTearDownSubTest)
690692
assert.True(t, suite.inTearDownTest)
691693
assert.True(t, suite.inTearDownSuite)

0 commit comments

Comments
 (0)