Reputation: 355
Is there any nice solution to prevent testng from running tests from a specific class if tests from another specific class are currently being executed?
I have next suite:
<suite name="Name" parallel="classes" thread-count="5">
<test name="TestName">
<packages>
<package name="test.folder.*" />
</packages>
</test>
</suite>
Inside "folder" it has a lot of another folders with test classes which are executed in parallel by this suite.
But I need to specify somehow if, for example, test.folder.a1.A1Tests is running, test ng should wait until its finished, to run class test.folder.b1.B1Tests and choose some another class which is not in 'ban' list of specified class.
Let we explain one more time what I want.
The point is I have tests, which affect each other and I want save parallelism but don't want to execute them at the same time.
The only way that I found for now, its add to current suite one more test, to execute all affectable tests in single thread, and then execute another tests in same way as now.
But I hope you have more prettier solution.
Upvotes: 1
Views: 839
Reputation: 2978
TestNG has already implemented "dependsOnMethods", "dependsOnGroups" options.
But your request is to have some kind of class level dependencies.
If you're trying to find a solution that will change the order of the tests while the tests are running, this won't work.
TestNG determines the specific order of the tests and keeps it within the whole execution. If the test turn comes, then you can simply wait for some conditions, but not take a new test instead of the current one.
So my suggestions will keep that assumption.
Option 1 - use separate testng xml configurations
It looks like you are already using this.
Moving all the tests that can't run safely in parallel into a separate testng xml with a non-parallel configuration and keeping the other tests running in parallel and then merging them into a single xml to run is a common solution that is easy to implement and maintain.
For most cases, it's nice enough compared to other existing alternatives because it's simple and straightforward.
Here is an example of how it might look like:
https://stackoverflow.com/a/70753897/5226491
Option 2 - implement your own synchronization
As I mentioned, there are no ready-made options.
You should:
You must implement this with all the necessary synchronizations to make sure it doesn't cause problems, deadlocks, etc.
You can start by implementing an ITestListener
that will add all running test classes to a thread-safe collection when the test starts and delete it when it finishes.
https://www.javadoc.io/doc/org.testng/testng/7.4.0/org/testng/ITestListener.html
https://www.javadoc.io/doc/org.testng/testng/7.4.0/org/testng/ITestResult.html#getTestClass--
I think option 2 takes time to implement and test (not to mention good programming experience) and there must be a really good reason for choosing it.
Upvotes: 1