-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add opt-in parallel test methods. #1703
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
base: master
Are you sure you want to change the base?
Conversation
Which of the many parallel test bugs this this aim to fix? #187 is about the parallel tests continuing to run after the suite has returned, which can cause failing tests to be marked as passing. #934 covers teardown being run while the parallel tests are still running. I've tried to convert such a situation to use this PR and it now panics: package kata_test
import (
"runtime"
"testing"
"github.com/stretchr/testify/suite"
)
type My struct {
suite.Suite
}
func (m *My) TestSequntial() {
m.Assert().True(true)
// passes during suite
}
func (m *My) ParallelTestPass() {
m.T().Parallel()
runtime.Gosched()
m.Assert().True(true)
// passes after suite
}
func (m *My) ParallelTestFail() {
m.T().Parallel()
runtime.Gosched()
m.Assert().True(false) // fails after suite
}
func TestIfy(t *testing.T) {
suite.Run(t, &My{})
}
My example is pretty racy, it can also fail before go exits and generate a warning, and it can also attribute the failure to the wrong test. |
@brackendawson The underlying problem is that the suite’s T() method can’t work in parallel tests. This changeset doesn’t fix that; instead, it just provides an alternative path to achieve parallel methods. Your Also, anything that references the suite’s internal The fixed suite behaves as I think you expect:
I’ve added guard rails to ensure that Parallel methods take the required A guard-rail might also be added that introspects the call stack (like this) to prevent folks from calling T() or Assert() from within parallel methods, but it’s potentially messy. I also realized that the suite will need an alternate solution for Run() in parallel tests. Before I do that, though, please share your thoughts on the above when you can. Thanks! |
I see, so the feature comes with some caveats:
Given this removes a lot of the functionality of suite from these methods, basically the only thing you should touch on receiver is any instance variables or methods you defined yourself, this is a very tenuous version of suite. It's also quite hard to document and it's likely people will use it incorrectly. I'm sorry to say don't think I'm sold. |
What functionality are you talking about? It seems to me a fairly trivial syntactic difference … and one that breaks no existing code. For me, anyhow, the real purposes of Suite are the before/after hooks and recursive grouping of test functions. Those remain intact and still quite natural.
Currently there are guard rails in place to prevent the following misuses up-front:
The only other avenue to “easy” misuse is
Fair enough, but the misuse prevention will also guide users to proper usage, which seems to me to mitigate the impact here. |
@brackendawson FYI, I’ve left the branch failing to illustrate the guard rail. |
I see, though it's quite ambiguous. Avoid? Assert vs assert? Also the assert package's functions are not equivalent. Better wording might be:
Though the difficulty of documenting this, and the effort the documentation and validation has to go to to steer people away from APIs that they must no longer use, but which are still available, tells me that this is not an optimal solution. I really don't relish the idea of having to support this. |
Summary
Add “ParallelTest” methods that run in parallel.
Changes
SetupParallelTestSuite
,TearDownParalleltestSuite
,BeforeParallelTest
, &AfterParallelTest
interfaces that allow the relevant suite methods to accept a*testing.T
.ParallelTest*
methods and, if present, execute them after the other methods in aparallel
subtest.Motivation
Issue #187 has vexed testify users for almost a decade. The library is too widely used for breaking changes such as PR #1109. This changeset proposes an alternative solution that preserves backward compatibility while allowing users to opt-in to the new functionality.
For example usage, see the new
suite_parallel_test.go
file (derived from PR #1109’s tests).Related issues
Closes #187.