testify
testify copied to clipboard
Trying to run tests in parallel with suite fails
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
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, 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 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.