简体   繁体   中英

TestNG parallel Execution with DataProvider

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.

You need to use dataproviderthreadcount . The threadpoolsize and invocationcount values are not required. See details here .

Try to set the thread pool in following way:

@BeforeClass
public void setupClassName(ITestContext context) {
    context.getCurrentXmlTest().getSuite().setDataProviderThreadCount(5);
    context.getCurrentXmlTest().getSuite().setPreserveOrder(false);
}

You can achieve this by adding extra configuration 'parallel=true', along with the name of the DataProvider, in its definition. An example is as follows:

@DataProvider(name="InvalidLoginDataProvider", parallel = true)
public Object[][] myDataProviderMethod(){
...
...
}

As per TestNG documentation, the @Test thread pool (created using invocationCount and threadPoolSize parameters in @Test) and Data provider thread pool are different and managed differently.
So, to specify how many threads in Data provider thread pool, one has to add the following configuration in testng.xml file.

<suite name="Suite1" data-provider-thread-count="20" >
...
...
</suite> 

HTH!

In testng.xml you can set thread count for the dataprovider via data-provider-thread-count="3"

<suite name="Manage" data-provider-thread-count="3" >
    <test name="Manage data tests">
        <classes>
            <class name="uk.example.ExampleTest"></class>
        </classes>
    </test>
</suite>

I think there is a way to set it on annotation level, it should be add on the DataProvider's :

@DataProvider(name="quick-screen-list", parallel = true)
        public Object[][] quickScreenDataProvider() {
           .....

Currently only one thread is getting used as you have define invocationCount as 1, if you change it to 3 then three workers thread will get used.

invocationCount :- The number of times this method should be invoked.

threadPoolSize :- The size of the thread pool for this method. The method will be invoked from multiple threads as specified by invocationCount. Note: this attribute is ignored if invocationCount is not specified.

Also,

You can also specify that a @Test method should be invoked from different threads. You can use the attribute threadPoolSize to achieve this result:

@Test(threadPoolSize = 3, invocationCount = 10,  timeOut = 10000)
public void testServer() {

In this example, the function testServer will be invoked ten times from three different threads. Additionally, a time-out of ten seconds guarantees that none of the threads will block on this thread forever.

More info can be found here

hi I am getting below error when i am trying to handle with data provider thread count .How to resolve the issue

java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated. Make sure to release the connection before allocating another one.

Data provider can be run parallel with the help of 'Parallel' Attribute in Java class and 'data-provider-thread-count' attribute in xml.

XML code-

<suite name="Suite1" data-provider-thread-count="2">

Java Code-

@DataProvider(parallel = true, name = "testNG")

Refer below url with Example.

https://testng-in-selenium-yogen.blogspot.com/p/data-provider-with-parallel-attribute.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM