I have a single test which receives data from data provider. I would like this test to run in parallel with different values from data provider .
I tried an approach like :
public class IndependentTest
{
@Test(dataProvider = "dp1" ,threadPoolSize=3,invocationCount=1)
public void testMethod(int number)
{
Long id = Thread.currentThread().getId();
System.out.println("HELLO : " + id);
}
@DataProvider(name = "dp1",parallel=true)
public Object[][] dp1() {
return new Object[][] {
new Object[] { 1 },
new Object[] { 2 },
new Object[] { 3 },
new Object[] { 4 },
new Object[] { 5 },
new Object[] { 6 },
new Object[] { 7 },
new Object[] { 8 }
};
}
}
The output i received is :
HELLO : 10
HELLO : 12
HELLO : 17
HELLO : 11
HELLO : 16
HELLO : 14
HELLO : 13
HELLO : 15
Spawned 10 threads while i specified 5 in the thread pool size . Could you please tell what has to be added to the above snippet to control the data provider thread pool size .
Note here that test method is run on data in same sequence in which DataProvider pass it. The reason behind this is that a DataProvider annotated method has an attribute named “parallel” whose default value is set to “false”. We can run it parallel as well by setting attribute “parallel” value as true.
To trigger parallel test execution in TestNG, i.e., run tests on separate threads, we need to set the parallel attribute. This attribute accepts four values: methods – runs all methods with @Test annotation in parallel mode. tests – runs all test cases present inside <test> tag in the XML in parallel mode.
Allow Multi-Threaded Tests: Using the parallel execution in TestNG, we can allow multiple threads to run simultaneously on the test case providing independence in the execution of different components of the software.
You need to use dataproviderthreadcount
. The threadpoolsize
and invocationcount
values are not required. See details here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With