Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test name refers to parent test name in TearDownSubTest #1465

Closed
SeriyBg opened this issue Sep 1, 2023 · 2 comments · Fixed by #1471
Closed

Test name refers to parent test name in TearDownSubTest #1465

SeriyBg opened this issue Sep 1, 2023 · 2 comments · Fixed by #1471
Labels
bug pkg-suite Change related to package testify/suite

Comments

@SeriyBg
Copy link

SeriyBg commented Sep 1, 2023

When having tests with subtests, the calling of the s.T().Name() in the TearDownSubTest will return the parent test name and not the subtest that has been executed
Having this test executed will print the correct sub-test names:

func (s *TestingSuite) TestWithSubtests() {
  s.Run("my subtest", func() {
    println("my subtest -> " + s.T().Name())
    s.Run("my sub subtest", func() {
      println("my sub subtest -> " + s.T().Name())
    })
  })
}

Output:

=== RUN   TestingSuite/TestWithSubtests
=== RUN   TestingSuite/TestWithSubtests/my_subtest
my subtest -> TestSuite/TestHazelcastExternalSmartClientLoadBalancer/my_subtest
=== RUN   TestingSuite/TestWithSubtests/my_subtest/my_sub_subtest
my sub subtest -> TestSuite/TestHazelcastExternalSmartClientLoadBalancer/my_subtest/my_sub_subtest

And I would expect that in the TearDownSubTest, the s.T().Name() should be consistent with these names. However, it always refers to the parent test name, i.e., TestingSuite/TestWithSubtests for the TestingSuite/TestWithSubtests/my_subtest, and TestingSuite/TestWithSubtests/my_subtest for the TestingSuite/TestWithSubtests/my_subtest/my_sub_subtest.

func (s *TestingSuite) TearDownSubTest() {
  println("TearDownSubTest -> " + s.T().Name())
}

Output:

TearDownSubTest -> TestSuite/TestHazelcastExternalSmartClientLoadBalancer/my_subtest
TearDownSubTest -> TestSuite/TestHazelcastExternalSmartClientLoadBalancer

Environment:
Go 1.19
Testify 1.8.4

@tscales
Copy link
Contributor

tscales commented Sep 1, 2023

https://github.com/stretchr/testify/blob/master/suite/suite.go#L103-L108

this should be resolved if SetT is moved to after the if statement.

@linusbarth
Copy link
Contributor

To be consistent with SetupTest and TearDownTest and also to be "symmetrical", the SetupSubTest should also be moved to after the SetT function.
That means: https://github.com/stretchr/testify/blob/master/suite/suite.go#L99-L101
to before line 112: https://github.com/stretchr/testify/blob/master/suite/suite.go#L112

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug pkg-suite Change related to package testify/suite
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants