Forbid TestNG to execute specific test classes in same time

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:

  • determine which class depends on a list of other classes
  • track all the running classes
  • resolve dependencies: check if the test method belongs to some class that must wait for other blocking classes to finish their work

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.