Search code examples
testngthreadpooltestng-dataprovider

Facing issue with parallel test execution in TestNG when using @DataProvider with TestNG XML


I've written a TestNG class which contains two methods (one@DataProvider, one @Test)

public class BaseTestNG extends BaseTest{ 
    @DataProvider(name="Demo", parallel=true)
    public Object[][] getData() {
        Object[][] data = new Object[4][2];
        data[0][0] = "sampleuser1";
        data[0][1] = "abcdef";
        data[1][0] = "testuser2";
        data[1][1] = "zxcvb";
        data[2][0] = "guestuser3";
        data[2][1] = "pass123";
        data[3][0] = "guestuser4";
        data[3][1] = "pass123";
        return data;
    }
    @Test(dataProvider="Demo")
    public void BaseTestNG(String username, String password)  {
        System.out.println(username + " :: " & password)
        Thread.currentThread.Sleep(5000);
    }
}

In testng.xml I've set parameter thread-count to "2". But when I run the testng.xml as a TestNGSuite, 4 instances of @Test are getting launched simultaneously instead of 2 threads.

How can I restrict only to number of thread-count mentioned in testing.xml and not on the amount of TestData passed from @DataProvider?


Solution

  • TestNG has two attributes in the TestNG suite xml file that lets you control threadpool size.

    • thread-count - This lets you control the number of threads when you enable parallelism (tests|classes|methods). This if not specified has a default value of 5
    • data-provider-thread-count - This lets you control the number of threads when a data provider is involved. This if not specified has a default value of 10

    To fix your problem you can consider setting data-provider-thread-count attribute in your testng-suite.xml file to 2. After that TestNG should spawn only two threads.

    Please note: You would need to create a suite xml file which includes your test class. Only then this will work.

    Please refer here for more details on the TestNG DTD.