//
you're reading...
TestNG

Parallel execution of multiple TestNG suites

If we would like to run more than one TestNG suite in parallel, here’s how to go about doing it.

For the sake of convenience am going to conveniently assume that your project is a Maven project.

We first start by defining two properties in our pom file – One property to accept a list of comma separated suite file names and the other property to dynamically help us control the suite’s thread pool size.

<properties>
    <threads>2</threads>
    <file>src/test/resources/default.xml</file>
</properties>

We now reference the above two properties in our surefire plugin as shown below :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.15</version>
    <configuration>
        <suiteXmlFiles>${file}</suiteXmlFiles>
        <skipTests>false</skipTests>
        <properties>
            <property>
                <name>suitethreadpoolsize</name>
                <value>${threads}</value>
            </property>
        </properties>
    </configuration>
</plugin>

For the sake of completeness here’s how our test classes look like

TestCaseOneForSuiteOne.java

public class TestCaseOneForSuiteOne {
    @Test
    public void testMethodOneForSuiteOne() {
        printer();
    }

    @Test
    public void testMethodTwoForSuiteOne() {
        printer();
    }

    private void printer() {
        ITestResult result = Reporter.getCurrentTestResult();
        String name = result.getTestClass().getName() + "." + result.getMethod().getMethodName() + "()  on Thread #"
            + Thread.currentThread().getId();
        Reporter.log(name + " ran.", true);
    }
}

TestCaseOneForSuiteTwo.java

public class TestCaseOneForSuiteTwo {
    @Test
    public void testMethodOneForSuiteTwo() {
        printer();
    }

    @Test
    public void testMethodTwoForSuiteTwo() {
        printer();
    }

    private void printer() {
        ITestResult result = Reporter.getCurrentTestResult();
        String name = result.getTestClass().getName() + "." + result.getMethod().getMethodName() + "()  on Thread #"
            + Thread.currentThread().getId();
        Reporter.log(name + " ran.", true);
    }
}

The two suite files look like below :

suite-one-with-execution.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite-1" parallel="false">
  <test name="Test-1">
    <classes>
      <class name="organized.chaos.testng.execute.TestCaseOneForSuiteOne"/>
    </classes>
  </test>
</suite>

suite-two-with-execution.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite-2" parallel="false">
    <test name="Test-2">
        <classes>
            <class name="organized.chaos.testng.execute.TestCaseOneForSuiteTwo"/>
        </classes>
    </test>
</suite>

Now we can pass in our desired suite file names in runtime as below from a command line :

mvn clean test -Dfile=src/test/resources/suite-one-with-execution.xml,src/test/resources/suite-two-with-execution.xml

Here’s how the output looks :

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
organized.chaos.testng.execute.TestCaseOneForSuiteTwo.testMethodOneForSuiteTwo()  on Thread #11 ran.
organized.chaos.testng.execute.TestCaseOneForSuiteOne.testMethodOneForSuiteOne()  on Thread #10 ran.
organized.chaos.testng.execute.TestCaseOneForSuiteTwo.testMethodTwoForSuiteTwo()  on Thread #11 ran.
organized.chaos.testng.execute.TestCaseOneForSuiteOne.testMethodTwoForSuiteOne()  on Thread #10 ran.
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.652 sec - in TestSuite

Results :

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

If we would like to change the thread pool size value we can change it via the runtime argument

mvn clean test -Dfile=src/test/resources/suite-one-with-execution.xml,src/test/resources/suite-two-with-execution.xml -Dthreads=1

Here’s how the output looks like

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
organized.chaos.testng.execute.TestCaseOneForSuiteOne.testMethodOneForSuiteOne()  on Thread #1 ran.
organized.chaos.testng.execute.TestCaseOneForSuiteOne.testMethodTwoForSuiteOne()  on Thread #1 ran.
organized.chaos.testng.execute.TestCaseOneForSuiteTwo.testMethodOneForSuiteTwo()  on Thread #1 ran.
organized.chaos.testng.execute.TestCaseOneForSuiteTwo.testMethodTwoForSuiteTwo()  on Thread #1 ran.
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.303 sec - in TestSuite

Results :

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
Advertisements

Discussion

8 thoughts on “Parallel execution of multiple TestNG suites

  1. Where is default.xml and what it should contain?

    Posted by ABC | April 20, 2016, 4:42 am
  2. There is another alternative to achieve same thing (Running suites and tests in parallel at the same time). Following is the way to define the test suites to achieve this.

    TestNg Suite-1
    —————

    TestNG Suite-2
    —————

    Global Suite
    ————

    Posted by santhosh soma | May 25, 2016, 9:18 pm
  3. I have 2 xml just like above- 1) suite-one-with-execution.xml and 2)suite-two-with-execution.xml

    My question is related to the surefire-reports that gets generated.

    When I run the first xml file,surefire-reports folder has details about the tests run in the 1st xml file.
    When I run the second xml file,surefire-reports folder has details about the tests run in the 2nd xml file.

    The problem that I am facing is that, the report generated using the 1st xml file goes away after the 2nd xml file is run which is creating a problem for me. I am trying to create a dashboard but I am unable to do so as details about the 1st report is no longer there after the 2nd xml is run.

    Please help

    Posted by Vinaya | June 1, 2016, 1:26 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: