16

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 .

Optimus Prime
  • 6,817
  • 5
  • 32
  • 60
sujith
  • 665
  • 2
  • 9
  • 22
  • You'll want to run tests in parallel, not the provider. In your build configure the parallel mode to `methods` and `threadCount` to 5. – Ben Manes Jul 20 '15 at 16:43
  • hi Ben , I have a single test which is supposed to run more than 10k times based on the number of values provided by the data provider . I would like to control the number of threads spawned when parallel=true is set in dataprovider. I found this commandline argument -dataproviderthreadcount to control dataprovider thread count. However i would like to know how this could be done using annotations – sujith Jul 20 '15 at 17:41
  • In my project's [build](https://github.com/ben-manes/caffeine/blob/master/caffeine/testing.gradle) I configure tests to run in parallel and, in total, execute 1.8M due to data providers on 827 test methods. I don't think you can configure this by the annotations and instead its a configuration passed into the runner. – Ben Manes Jul 20 '15 at 18:52

7 Answers7

22

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

Saikat
  • 14,222
  • 20
  • 104
  • 125
niharika_neo
  • 8,441
  • 1
  • 19
  • 31
  • hi niharika , Using dataproviderthreadcount solved my problem . However i am able to set this only as cmd line argument . Is there any way to pass the same at annotation level ? – sujith Jul 21 '15 at 14:38
  • 1
    Nope. No way from the annotation level. – niharika_neo Jul 22 '15 at 06:44
  • thank you clearing that there is no way to provide this at annotation level. Does this mean that all the tests that use any dataprovider will use the number of tests specified using dataproviderthreadcount ? Would it be possible to control the number of threads based on the test rather than letting all the tests use multiple threads? – sujith Jul 22 '15 at 12:20
  • AFAIK it is at the run level and cannot be controlled at test level - if I am understanding your question correctly – niharika_neo Jul 23 '15 at 07:24
  • Okay . That was the exact info i was looking for. – sujith Jul 23 '15 at 09:37
  • If you use a `maven-surefire-plugin`, you can define this property in plugin's configuration like `dataproviderthreadcount` and `5` enclosed in `` tag. – Vitali Plagov Sep 07 '18 at 12:50
  • Does this `dataproviderthreadcount` mean there will be one dedicated browser opened for each row. I mean 5 browsers will open for first 5 rows, run in parallel and then 5 again? – paul Aug 18 '20 at 11:11
  • @sujith i have similar usecase. Can you pls provide solution? thanks a lot! – raj Dec 02 '21 at 06:47
4

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);
}
sanitar4eg
  • 153
  • 1
  • 1
  • 13
4

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!

ChaM
  • 111
  • 1
  • 4
3

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>
Igor Gladun
  • 1,422
  • 1
  • 8
  • 8
1

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

TheCodingFrog
  • 3,406
  • 3
  • 21
  • 27
  • Hi, I tried that approach but my question is specifically regarding parallel execution with data provider. I am trying to run the test parallelly using dataprovider. Specifying invocation count as 3 in this case would result in the method being invoked 10 times for each input from data provider which account for 8 * 3 = 24 runs. – sujith Jul 20 '15 at 17:20
  • Data provider will not help in running it parallel.. it's a way to describe your input which call you'er test 1 by 1 using those inputs - IMHO – TheCodingFrog Jul 21 '15 at 02:09
  • Hi , dataproviderthreadcount can be used to simulate parallel execution of the single test method with the data instances that come from dataprovider. Data provider thread pool is different from the test thread pool in testng. However dataproviderthreadcount is specified as a cmd line argument . I would like to know if it is possible to specify this at annotation level. – sujith Jul 21 '15 at 14:40
1

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() {
           .....
yu yang
  • 21
  • 2
0

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.

Yasin Patel
  • 5,624
  • 8
  • 31
  • 53
  • 1
    This does not provide an answer to the question. You can [search for similar questions](//stackoverflow.com/search), or refer to the related and linked questions on the right-hand side of the page to find an answer. If you have a related but different question, [ask a new question](//stackoverflow.com/questions/ask), and include a link to this one to help provide context. See: [Ask questions, get answers, no distractions](//stackoverflow.com/tour) – Yunnosch Feb 14 '20 at 06:45
  • 1
    @YasinPatel Improving the formatting of a question which is in the place where only an answer is allowed gives the wrong impression to the author that misusing StackOverflow like this is OK. Please don't. Also if you do improve (other) posts, then please don't stop at formatting and also handle typos, lack of punctuation and misplaced whitespace. As it is, your edit gives the impression that you neither read the post itself nor the existing comment. – Yunnosch Feb 14 '20 at 07:04