0
votes

There are 5 test classes in total. Each of them are initialized using @Factory(dataprovider = "data"). What I want to achieve is, the test methods in each test class should run in parallel with the dataprovider instance. Also, the test classes should be running in parallel.

Something like the following. TestClass1 should be running the dataprovider instances in parallel. So, all the methods of test class TestClass1 will be running in parallel for the dataprovider instances.

Also, like TestClass1, there are 2 other test classes. I am expecting the same behaviour from them, and all 3 test classes running in parallel.

With the arrangement of test classes and parameters in the testng.xml giving

data-provider-thread-count="10" parallel="instances" thread-count="5""

at suite level and at test level,

parallel="instances" thread-count="5"

the observed behaviour is that, TestClass1 instances created by the dataprovider instances are running in parallel. TestClass2 and TestClass3 have not started execution. Soon after TestClass1 ends, TestClass2 starts execution in same fashion as TestClass1, followed by TestClass3.

What change do I need to make to achieve the intended behaviour.

Thanks, in advance.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.reporters.EmailableReporter2;

@Listeners({ TestExecutionListener.class, EmailableReporter2.class })
public class TestClass {

    private int value;

    @Factory(dataProvider = "data", dataProviderClass = TestClass.class)
    public TestClass(final int value) {
        this.value = value;
    }

    @Test(alwaysRun = true)
    public void testOdd() {
        Assert.assertTrue(value % 2 != 0);
    }

    @Test(alwaysRun = true)
    public void testEven() {
        Assert.assertTrue(value % 2 == 0);
    }

    @DataProvider(name = "data")
    public static Iterator<Object[]> data() {

        List<Object[]> list = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            list.add(new Object[] { i });
        }
        return list.iterator();
    }
}

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="mobile rc automation suite"
    data-provider-thread-count="10" parallel="instances" thread-count="5">

    <test name="test_1" parallel="instances"
        thread-count="5">
        <classes>
            <class name="com.test.TestClass1" />
        </classes>
    </test>

    <test name="test_2" parallel="instances"
        thread-count="5">
        <classes>
            <class name="com.test.TestClass2" />
        </classes>
    </test>

    <test name="test_3" parallel="instances"
        thread-count="5">
        <classes>
            <class name="com.test.TestClass3" />
        </classes>
    </test>

</suite>
2
Basically you want to achieve full parallelism?niharika_neo
@niharika_neo : Yes.Sarath
You can make it parallel="methods". Remove the parallel="instances" at test level. Keep only at suite level.niharika_neo

2 Answers

2
votes

I was finally able to run @Factory test classes that are present in <test /> in parallel using the given testNG xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="mobile rc automation suite"
    data-provider-thread-count="10" parallel="tests" thread-count="5">

    <test name="test_1" parallel="instances"
        thread-count="5">
        <classes>
            <class name="com.test.TestClass1" />
        </classes>
    </test>

    <test name="test_2" parallel="instances"
        thread-count="5">
        <classes>
            <class name="com.test.TestClass2" />
        </classes>
    </test>

    <test name="test_3" parallel="instances"
        thread-count="5">
        <classes>
            <class name="com.test.TestClass3" />
        </classes>
    </test>

</suite>
1
votes

You can make it parallel="methods". Remove the parallel="instances" at test level. Keep only at suite level.