Jon H
Jon H

Reputation: 434

Trying to launch parallel TestNG tests from Maven using Surefire

I am trying to to launch parallel TestNG tests from Maven using Surefire, but with little success so far.

My suite has 4 tests, but Maven is so far just running the tests one at a time, in series. Only when one test finishes does the next one start.

I would be very grateful if you could look at the config I am using and let me know if you spot anything amiss or have any general suggestions.

This is how surefire is configured in pom.xml: -

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20.1</version>
        <configuration>
            <parallel>classes</parallel>
            <threadCount>3</threadCount>
            <testFailureIgnore>true</testFailureIgnore>
            <suiteXmlFiles>
                <suiteXmlFile>src/main/MyTestSuite.xml</suiteXmlFile>
            </suiteXmlFiles>
        </configuration>
    </plugin>

This is my suite file (MyTestSuite.xml): -

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

    <suite name="My Custom suite" parallel="classes">
        <parameter name="driver.type" value="CHROME"/>
        <parameter name="environment" value="DEV"/>
        <parameter name="timeout" value="5000"/>
        <parameter name="maxAttempts" value="20"/>
        <parameter name="logLevel" value="INFO"/>
        <parameter name="driver.quit" value="true"/>
        <suite-files>
            <suite-file path="./Test1.xml"/>
            <suite-file path="./Test2.xml"/>
            <suite-file path="./Test3.xml"/>
            <suite-file path="./Test4.xml"/>
        </suite-files>
    </suite>

And here is an example of one of the individual suite files (Test1.xml): -

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test Suite 1" parallel="classes">
    <test verbose="10" name="Test1" annotations="JDK">
        <classes>
            <class name="com.me.test.Test1" />
        </classes>
    </test>
</suite>

The tests run successfully, but in series, not parallel. Any suggestions would be most welcome. I have had no problem getting surefire to run my Cucumber tests in parallel, but no luck so far with TestNG. Thank you for reading.

Upvotes: 1

Views: 4391

Answers (1)

Krishnan Mahadevan
Krishnan Mahadevan

Reputation: 14746

You are working with a suite of suites. By default when TestNG sees a suite of suites, it runs them in sequence unless instructed otherwise.

In your case, you can trigger parallel suite execution by configuring your surefire plugin as below :

First add up properties as below

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

and then define the surefire plugin as below:

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

You can now run your tests using the command mvn clean test -Dthreads=1

For more details on parallel execution and running suites in parallel without using a suite of suites, you can refer to my blog post here.

Upvotes: 2

Related Questions