Skip to content

Trying to run tests in parallel with suite fails #1253

Closed as not planned
Closed as not planned

Description

@pantelis-karamolegkos

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

Activity

  1. brackendawson commented on Aug 17, 2022

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

  2. added
    pkg-suite
    on Jul 25, 2023
  3. Antonboom commented on Jun 6, 2024

    @Antonboom

    @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

  4. dolmen commented on Jun 6, 2024

    @dolmen
    Collaborator

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    concurrencypkg-suiterejected/duplicate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions