Running multiple instances of test class in parallel using @Factory

2,572 views
Skip to first unread message

Karthigeyan Diraviam

unread,
Jan 24, 2013, 9:08:18 PM1/24/13
to testng...@googlegroups.com
Hi,

I'm trying to run multiple instances of my test class in parallel. I searched thru some forums and couldn't find a solution for this. Can someone help me with this?

Here's my Factory class and methods:
    @DataProvider(name="versions", parallel=true)
    public Iterator<Object[]> versionsInfo() {
        List<Object[]> data = new ArrayList<Object[]>();
        List<String> versions = Arrays.asList("v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", "v10");
        for ( String version : versions ) {
            data.add(new Object[]{new parallelTestExample(version)});
        }
        return data.iterator();
    }

    @Factory(dataProvider = "versions")
    public Object[] parallelTestExampleFactoryProvider(Object testObject) {
        List<Object> result =  new ArrayList<Object>();
        result.add(testObject);
        return result.toArray();
    }

My test class looks like this:
public class parallelTestExample {
    private String version;

    public parallelTestExample() {
        version = "v4";
    }

    public parallelTestExample(String version) {
        this.version = version;
    }

    @Test(groups={"Functional"})
    public void myTestCase_0001() {
        String testId = Thread.currentThread().getStackTrace()[1].getMethodName();
        logger.debug("INFO: Entering " + testId);
        logger.debug("INFO: Testing  Version " + version + " using Thread id : " + Thread.currentThread().getId());
        logger.debug("INFO: Exiting " + testId);
    }
}

I see every instance of this class is running sequentially. How can I make it run in parallel? Appreciate any help on this.

Thanks
Regards,
Karthi.


Cédric Beust ♔

unread,
Jan 24, 2013, 9:45:28 PM1/24/13
to testng...@googlegroups.com

On Thu, Jan 24, 2013 at 6:08 PM, Karthigeyan Diraviam <karthid...@gmail.com> wrote:
I see every instance of this class is running sequentially.

How exactly are you seeing this? Printing the thread id's?

-- 
Cédric

Karthigeyan Diraviam

unread,
Jan 24, 2013, 10:11:36 PM1/24/13
to testng...@googlegroups.com
Yes, I see version v1 .. v10 in sequence and thread id is 9 for all test output.

Regards,
Karthi.


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Krishnan Mahadevan

unread,
Jan 24, 2013, 10:15:12 PM1/24/13
to testng...@googlegroups.com
How are you running your tests? Via eclipse?
If yes, then have you setup any global testng template file under project > properties > testng ( or )
Window > preferences > testng 

?


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

Karthigeyan Diraviam

unread,
Jan 24, 2013, 10:24:50 PM1/24/13
to testng...@googlegroups.com
No, I'm running in command line, something like this:
java -Dkey=val -Dkey=val -cp  <path to jars> org.testng.TestNG ./src/test/resources/test-parallelTestExample.xml -d <output directory> -listener org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter

XML File:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="test-parallelTestExample" verbose="1" parallel="false">
    <test name="parallelTestExample_Functional">
        <groups>
            <run>
                <include name="Functional" />
            </run>
        </groups>
        <classes>
            <class name="com.company.product.test.common.FactoryProvider">
                <methods>
                    <include name="parallelTestExampleFactoryProvider"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>

Regards,
Karthi.


To unsubscribe from this group, send email to testng-users...@googlegroups.com.

Krishnan Mahadevan

unread,
Jan 24, 2013, 10:29:24 PM1/24/13
to testng...@googlegroups.com
Take a closer look at your xml. 

You have disabled parallelism [ parallel=false ]
Get rid of that and try again. 

Cédric Beust ♔

unread,
Jan 24, 2013, 10:42:23 PM1/24/13
to testng...@googlegroups.com
And print thread id's if you want to convince yourself that your tests are running in parallel.


-- 
Cédric

Karthigeyan Diraviam

unread,
Jan 28, 2013, 7:18:37 PM1/28/13
to testng...@googlegroups.com
Hi,

Here are my changes:
// @Test(singleThreaded = true)
public class parallelTestExample {
    private String version = null;

    @BeforeClass(alwaysRun=true)
    public void beforeClass() {
        if ( version == null )
            version = "v4";
    }

    @DataProvider(name = "parallelTest", parallel = true)
    public Iterator<Object[]> parallelTest() {

        List<Object[]> data = new ArrayList<Object[]>();
        List<String> versions = Arrays.asList("v1", "v2", "v3", "v4");

        for ( String version : versions ) {
            data.add(new Object[]{ version });
        }
        return data.iterator();
    }

    @Factory(dataProvider = "parallelTest")

    public parallelTestExample(String version) {
        this.version = version;
    }

    @Test(description="Parallel test 0001", groups = "Functional")
    public void parallelTest_0001() {
        logger.debug("Begin: Method: " + Thread.currentThread().getStackTrace()[1].getMethodName() + ", version: " +
                version + ", Thread: " + Thread.currentThread().getId());
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            Assert.fail("Sleep failed.", e);
        }
        logger.debug("End: Method: " + Thread.currentThread().getStackTrace()[1].getMethodName() + ", version: " +
                version + ", Thread: " + Thread.currentThread().getId());
    }

    @Test(description="Parallel test 0002", groups = "Functional")
    public void parallelTest_0002() {
        logger.debug("Begin: Method: " + Thread.currentThread().getStackTrace()[1].getMethodName() + ", version: " +
                version + ", Thread: " + Thread.currentThread().getId());
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            Assert.fail("Sleep failed.", e);
        }
        logger.debug("End: Method: " + Thread.currentThread().getStackTrace()[1].getMethodName() + ", version: " +
                version + ", Thread: " + Thread.currentThread().getId());
    }

    @Test(description="Parallel test 0003", groups = "Functional")
    public void parallelTest_0003() {
        logger.debug("Begin: Method: " + Thread.currentThread().getStackTrace()[1].getMethodName() + ", version: " +
                version + ", Thread: " + Thread.currentThread().getId());
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            Assert.fail("Sleep failed.", e);
        }
        logger.debug("End: Method: " + Thread.currentThread().getStackTrace()[1].getMethodName() + ", version: " +
                version + ", Thread: " + Thread.currentThread().getId());

    }
}

<?xml version="1.0" encoding="UTF-8"?>
<suite name="test-parallelTestExample" verbose="1" parallel="methods">

    <test name="parallelTestExample_Functional">
        <groups>
            <run>
                <include name="Functional" />
            </run>
        </groups>
        <classes>
            <class name="com.company.product.test.suite.function.parallelTestExample"/>
        </classes>
    </test>
</suite>

When I run this test, I get following output:
[2013/01/28:23:51:34][DEBUG][parallelTestExample.java:73]: Begin: Method: parallelTest_0003, version: v1, Thread: 9
[2013/01/28:23:51:34][DEBUG][parallelTestExample.java:60]: Begin: Method: parallelTest_0002, version: v1, Thread: 10
[2013/01/28:23:51:34][DEBUG][parallelTestExample.java:47]: Begin: Method: parallelTest_0001, version: v1, Thread: 11
[2013/01/28:23:51:34][DEBUG][parallelTestExample.java:73]: Begin: Method: parallelTest_0003, version: v2, Thread: 12
[2013/01/28:23:51:34][DEBUG][parallelTestExample.java:60]: Begin: Method: parallelTest_0002, version: v2, Thread: 13
[2013/01/28:23:51:34][DEBUG][parallelTestExample.java:54]: End: Method: parallelTest_0001, version: v1, Thread: 11
[2013/01/28:23:51:34][DEBUG][parallelTestExample.java:47]: Begin: Method: parallelTest_0001, version: v2, Thread: 11
[2013/01/28:23:51:35][DEBUG][parallelTestExample.java:67]: End: Method: parallelTest_0002, version: v1, Thread: 10
[2013/01/28:23:51:35][DEBUG][parallelTestExample.java:73]: Begin: Method: parallelTest_0003, version: v3, Thread: 10
[2013/01/28:23:51:35][DEBUG][parallelTestExample.java:67]: End: Method: parallelTest_0002, version: v2, Thread: 13
[2013/01/28:23:51:35][DEBUG][parallelTestExample.java:60]: Begin: Method: parallelTest_0002, version: v3, Thread: 13
[2013/01/28:23:51:35][DEBUG][parallelTestExample.java:54]: End: Method: parallelTest_0001, version: v2, Thread: 11
[2013/01/28:23:51:35][DEBUG][parallelTestExample.java:47]: Begin: Method: parallelTest_0001, version: v3, Thread: 11
[2013/01/28:23:51:35][DEBUG][parallelTestExample.java:80]: End: Method: parallelTest_0003, version: v1, Thread: 9
[2013/01/28:23:51:35][DEBUG][parallelTestExample.java:73]: Begin: Method: parallelTest_0003, version: v4, Thread: 9
[2013/01/28:23:51:35][DEBUG][parallelTestExample.java:80]: End: Method: parallelTest_0003, version: v2, Thread: 12
[2013/01/28:23:51:35][DEBUG][parallelTestExample.java:60]: Begin: Method: parallelTest_0002, version: v4, Thread: 12
[2013/01/28:23:51:35][DEBUG][parallelTestExample.java:54]: End: Method: parallelTest_0001, version: v3, Thread: 11
[2013/01/28:23:51:35][DEBUG][parallelTestExample.java:47]: Begin: Method: parallelTest_0001, version: v4, Thread: 11
[2013/01/28:23:51:35][DEBUG][parallelTestExample.java:67]: End: Method: parallelTest_0002, version: v3, Thread: 13
[2013/01/28:23:51:35][DEBUG][parallelTestExample.java:54]: End: Method: parallelTest_0001, version: v4, Thread: 11
[2013/01/28:23:51:35][DEBUG][parallelTestExample.java:80]: End: Method: parallelTest_0003, version: v3, Thread: 10
[2013/01/28:23:51:35][DEBUG][parallelTestExample.java:67]: End: Method: parallelTest_0002, version: v4, Thread: 12
[2013/01/28:23:51:35][DEBUG][parallelTestExample.java:80]: End: Method: parallelTest_0003, version: v4, Thread: 9

However, my expectation is to run test methods parallelTest_0001, parallelTest_0002 and parallelTest_0003 in sequence for a given version and each version in separate thread. I would like see similar as below execution:
Begin: Method: parallelTest_0001, version: v1, Thread: 9
Begin: Method: parallelTest_0001, version: v2, Thread: 10
Begin: Method: parallelTest_0001, version: v3, Thread: 11
End: Method: parallelTest_0001, version: v1, Thread: 9
End: Method: parallelTest_0001, version: v2, Thread: 10
End: Method: parallelTest_0001, version: v3, Thread: 11
Begin: Method: parallelTest_0002, version: v1, Thread: 9
Begin: Method: parallelTest_0002, version: v2, Thread: 10
Begin: Method: parallelTest_0002, version: v3, Thread: 11
End: Method: parallelTest_0002, version: v1, Thread: 9
End: Method: parallelTest_0002, version: v2, Thread: 10
End: Method: parallelTest_0002, version: v3, Thread: 11
Begin: Method: parallelTest_0003, version: v1, Thread: 9
Begin: Method: parallelTest_0003, version: v2, Thread: 10
Begin: Method: parallelTest_0003, version: v3, Thread: 11
End: Method: parallelTest_0003, version: v1, Thread: 9
End: Method: parallelTest_0003, version: v2, Thread: 10
End: Method: parallelTest_0003, version: v3, Thread: 11

How can I achieve this?

When I have @Test(singleThreaded = true), I see all tests for all versions are running in sequence. Let me know if I'm doing any thing wrong.

Thanks
Regards,
Karthi.

Karthigeyan Diraviam

unread,
Jan 29, 2013, 12:46:48 PM1/29/13
to testng...@googlegroups.com
Hi,

I would like to know a solution to run 2 instances of a class in parallel with all tests in an instance in sequence. Appreciate if someone can help me to achieve this. Let me know.

Thanks
Regards,
Karthi.

Krishnan Mahadevan

unread,
Jan 29, 2013, 2:15:10 PM1/29/13
to testng...@googlegroups.com
Use @Factory to produce TestClass Instances. 
Within the test class have your test methods depend on each other using dependsOnMethods attribute of the @Test annotation. Wouldn't that help?

Bhargava Ps

unread,
Nov 4, 2013, 3:49:30 AM11/4/13
to testng...@googlegroups.com
Hi
I have a problem with the @Factory as well. Can you please help with this question:

Krishnan Mahadevan

unread,
Nov 6, 2013, 8:06:48 PM11/6/13
to testng...@googlegroups.com
Have you tried using the suite xml attribute group-by-instances=true and see if that helps ? 
--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.

For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages