DependsOnMethods made parallel class use several threads
TestNG Version
6.9.12 and 6.9.13.8
Expected behavior
When setting TestNG to run tests in parallel by class, all the tests in a single class should be run in the same thread.
Actual behavior
If one of the test method depends on another, a new thread is created, which is really annoying for Selenium case for instance (BeforeClass open the browser, AfterClass kill it)
Test case sample
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="main" parallel="classes" thread-count="2" verbose="0" group-by-instances="false">
<test name="all">
<classes>
<class name="TestFoo"/>
</classes>
</test>
</suite>
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class TestFoo {
@BeforeClass
public void navigateTo() {
System.out.print("BeforeClass - ");
printThread();
}
private void printThread() {
System.out.println(Thread.currentThread().getName());
}
@AfterClass
public void die() {
System.out.print("AfterClass - ");
printThread();
}
@Test
public void t1() {
System.out.print("T1 - ");
printThread();
}
@Test(dependsOnMethods = "t1")
public void t2() {
System.out.print("T2 - ");
printThread();
}
}
BeforeClass - pool-1-thread-1
T1 - pool-1-thread-1
T2 - pool-1-thread-2
AfterClass - pool-1-thread-2
What I've tried:
- singleThreaded = true
- group-by-instance="true"
Is it working as expected without the dependsOnMethods?
@juherr yes, only 1 thread was used when I removed dependsOnMethods from the example above:
BeforeClass - pool-1-thread-1
T1 - pool-1-thread-1
T2 - pool-1-thread-1
AfterClass - pool-1-thread-1
It's working fine also if I set thread-count to 1.
Yes, the problem is located in GraphThreadPoolExecutor/DynamicGraph. But I don't know how to fix it for the moment. Don't expect a quick fix, sorry.
@juherr At least it's already good to know where the issue come from. Except remove the dependsOnMethods and refactor a bit my class, do you see a workaround?
In fact, dependsOn with parallel class can not work together. If it worked before, it was a side effect of another issue :/
I don't understand why you said dependsOn with parallel class cannot work together. Indeed, in this paralell mode, all the test methods inside a class has to be run in the same thread, so it does not affect the dependsOn, this is the theory. Perhaps, you said it cannot work because the code does not allow it?
Perhaps, you said it cannot work because the code does not allow it?
In the current state, yes. But it is supposed to work ;)
Hi @juherr
Any update on this? I'm facing the same problem due to parallel execution with dependsOnMethod. I had a look to graph/GraphThreadPoolExecutor.java but it looks like my basic knowledge won't be enough here.
Thank you!
@mathias21 Nope, nothing new for the moment. Sorry.
Hi @thibaut-sticky
I think I've found a workaround to solve this problem. It takes 2 dependencies: with the max amount of devices you are going to use at the same time, and also forcing you to release the session during @AfterClass method, but apparently it works. If you specify in your suite XML file "thread-count='2'", then you can define your suite as following:
<suite name="SmokeTest" configfailurepolicy="continue" parallel="classes" thread-count="2">
<test name="Test cases" >
<classes>
<class name="packageA.TestClass1"/>
<class name="packageA.TestClass2"/>
</classes>
</test>
<test name="Test cases2" >
<classes>
<class name="packageB.TestClass1"/>
<class name="packageB.TestClass2"/>
</classes>
</test>
</suite>
It is not the best solution, but apparently runs your tests with proper sorting.
@mathias21 Thx you for your workaround, but sadly in my case I cannot apply it. But at least I can see I'm not the only one using parallel and dependsOnMethod features 👍
i have the same problem with parallel=classes & using dependsOnMethod in one classe, and i do some prepare work using localThread var :(. it takes me a long time to find out the thread change. hope this bug could be fixed soon :)
The similar problem happens when using priority. Described in #1066
@juherr I am quite impacted by this issue as well, and saw you self-assigned the ticket. Does that mean you're actively working on a resolution? What kind of priority is this for you? This rather high for us... Thanks
Hi all
Do we know if this is not happening in a concrete version? I'm also using 6.9.12. I believe this is a structural issue, not a punctual bug or typo, am I right?
@sylvainbouxin "actively" is not the good word but it is on the top of the list and I planned to work on it unless someone has enough motivation. And I'm open to any help! 😉
@mathias21 Exactly! There are some similar issues and the root cause is often DynamicGraph which manages the order of methods and the group of methods when parallel is used. So, just one of the most important part of TestNG which generate test regression each time I have to modify it! 😨
@juherr nice challenge, indeed... I'm currently looking into this: I really need this fixed or write my own framework to manage the order of methods and classes with parallel execution.
@sylvainbouxin Nice! Feel free to send me emails if you need to exchange some extra things.
@juherr, you said:
Yes, the problem is located in GraphThreadPoolExecutor/DynamicGraph.
Can you elaborate on what the problem is exactly?