testify icon indicating copy to clipboard operation
testify copied to clipboard

Trying to run tests in parallel with suite fails

Open pantelis-karamolegkos opened this issue 2 years ago • 1 comments

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

pantelis-karamolegkos avatar Aug 17 '22 06:08 pantelis-karamolegkos

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.

brackendawson avatar Aug 17 '22 13:08 brackendawson

@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 (https://github.com/Antonboom/testifylint/pull/104)

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

Antonboom avatar Jun 06 '24 08:06 Antonboom

@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.

dolmen avatar Jun 06 '24 14:06 dolmen