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

Trying to run tests in parallel with suite fails #1253

Closed
pantelis-karamolegkos opened this issue Aug 17, 2022 · 3 comments
Closed

Trying to run tests in parallel with suite fails #1253

pantelis-karamolegkos opened this issue Aug 17, 2022 · 3 comments
Labels
concurrency pkg-suite Change related to package testify/suite rejected/duplicate

Comments

@pantelis-karamolegkos
Copy link

I have several tests using testify/suite and I execute them in parallel as follows


type IntegrationSuite struct {
	suite.Suite
}

func TestIntegrationSuite(t *testing.T) {
	suite.Run(t, &IntegrationSuite{})
}

func (is *IntegrationSuite) TestSomething() {
	is.T().Log("\tIntegration Testing something")
	for i := range myTestTable {
		i := i
		is.T().Run("Testing "+myTestTable[i].scenarioName, func(_ *testing.T) {
			is.T().Parallel()
...

func (is *IntegrationSuite) TestSomethingElse() {
	is.T().Log("\tIntegration Testing something else")
	for i := range myOtherTestTable {
		i := i
		is.T().Run("Testing "+myOtherTestTable[i].scenarioName, func(_ *testing.T) {
			is.T().Parallel()
...
		})

However this panics with

panic: testing: t.Parallel called multiple times [recovered]
        panic: testing: t.Parallel called multiple times
@brackendawson
Copy link
Collaborator

This happens because the value returned by is.T() is the *testing.T for the test, not for the subtest.

Maybe not a dupe but related to to #187, I think #1109 would fix this.

@Antonboom
Copy link

@brackendawson, hi!

I found that the pattern

s.T().Run("subtest", func(t *testing.T) {
    t.Parallel()
})

works fine (test).

But this is incompatible with any s.Run in the same test and generally encourages people to avoid s.Run, although I consider it best practice (Antonboom/testifylint#104)

What do you think is the example above valid? Or just black magic? Or I missed something?
Thanks

@dolmen
Copy link
Collaborator

dolmen commented Jun 6, 2024

@Antonboom Users are expecting AfterTest to run after test. We can't can't guarantee that if the test runs in another goroutine because it is Parallel.

So at this point, as a maintainer, it is safer to just discourage using Parallel subtests.

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

No branches or pull requests

4 participants