Skip to content

Commit

Permalink
improve: tests for asserting test names in subtests
Browse files Browse the repository at this point in the history
  • Loading branch information
linusbarth committed Oct 17, 2023
1 parent ac5cd69 commit c8efd3c
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions suite/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ type SuiteTester struct {
SetupSubTestRunCount int
TearDownSubTestRunCount int

SetupSubTestNames []string
TearDownSubTestNames []string

SuiteNameBefore []string
TestNameBefore []string

Expand Down Expand Up @@ -242,8 +245,8 @@ func (suite *SuiteTester) TestSubtest() {
for _, t := range []struct {
testName string
}{
{"first-subtest"},
{"second-subtest"},
{"first"},
{"second"},
} {
suiteT := suite.T()
suite.Run(t.testName, func() {
Expand All @@ -258,13 +261,13 @@ func (suite *SuiteTester) TestSubtest() {
}

func (suite *SuiteTester) TearDownSubTest() {
suite.TearDownSubTestNames = append(suite.TearDownSubTestNames, suite.T().Name())
suite.TearDownSubTestRunCount++
suite.Contains(suite.T().Name(), "subtest", "We should get the *testing.T for the test that is to be torn down")
}

func (suite *SuiteTester) SetupSubTest() {
suite.SetupSubTestNames = append(suite.SetupSubTestNames, suite.T().Name())
suite.SetupSubTestRunCount++
suite.Contains(suite.T().Name(), "subtest", "We should get the *testing.T for the test that is to be set up")
}

type SuiteSkipTester struct {
Expand Down Expand Up @@ -321,6 +324,12 @@ func TestRunSuite(t *testing.T) {
assert.Contains(t, suiteTester.TestNameBefore, "TestSkip")
assert.Contains(t, suiteTester.TestNameBefore, "TestSubtest")

assert.Contains(t, suiteTester.SetupSubTestNames, "TestRunSuite/TestSubtest/first")
assert.Contains(t, suiteTester.SetupSubTestNames, "TestRunSuite/TestSubtest/second")

assert.Contains(t, suiteTester.TearDownSubTestNames, "TestRunSuite/TestSubtest/first")
assert.Contains(t, suiteTester.TearDownSubTestNames, "TestRunSuite/TestSubtest/second")

for _, suiteName := range suiteTester.SuiteNameAfter {
assert.Equal(t, "SuiteTester", suiteName)
}
Expand Down

0 comments on commit c8efd3c

Please sign in to comment.