Bhuvan Bhuvan - 3 months ago 30
Java Question

How to configure multi-threading in TestNG having @DataProvider, @Factory and one @Test method

I want to have multi-threading in my class, i.e., my

@Test
method should run in parallel.

The test class has one
@Test
annotated method, one
@DataProvider
and one
@Factory
method which is a constructor of my test-class.

I have also overridden
getTestName(ITest class)
method. My test-data has 28 scenarios.

EDIT

@Listeners com.loyalty.HTMLReporter.class
public class TestClass implemented ITest {

@DataProvider
public static Object[][] dp () {
//I read test data from a file. The test-data could vary from 2 to 1000
//scenarios.
Object tests [][] = new Object [testcaseIdMap.keyset().toArray().length][];

for (Object ket : testcaseIdMap.keySet().toArray()){
tests[i] = new Object[1];
tests[i][0] = key.toString();
i++;
}
return tests
}

@Factory (dataProvider = "dp")
public TestClass (String testcaseId) {
this.testcaseId = testcaseId;
}

@Test
public void TC () {
//some code
}

@Override
public String getTestName () {
return testcaseId;
}

@BeforeSuite
public void createDBConnection () {
// some code
}

@AfterSuite
public void failedTestCases () {
//some code
}

Answer

If you want to run your test methods parallel, create a test suite xml file like below

<suite name="Your suite name" parallel="methods" thread-count="2" >
    <test name="Your test name">
        <classes>
            <class name="packagename.classname" />
        </classes>
     </test>
</suite>

Set number of threads as per your need.

If you want to run your dataprovider parallel, then add parallel=true in the dataprovider annotation.

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