6

I need to run multiple test suites in parallel. One of the approaches being, to create a suite file as below -

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="AllTests" verbose="8"> <suite-files> <suite-file path="./Suite1.xml"></suite-file> <suite-file path="./Suite2.xml"></suite-file> </suite-files> </suite> 

Create a class as below -

import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.List; import javax.xml.parsers.ParserConfigurationException; import org.testng.xml.Parser; import org.testng.xml.XmlSuite; import org.testng.TestNG; import org.xml.sax.SAXException; public class RunSuitesInParallel{ public static void main(String[] args) throws FileNotFoundException, ParserConfigurationException, SAXException, IOException { TestNG testng = new TestNG(); testng.setXmlSuites((List <XmlSuite>)(new Parser("src"+File.separator+"test"+File.separator+"resources"+File.separator+"xml_Suites"+File.separator+"AllTests.xml").parse())); testng.setSuiteThreadPoolSize(2); testng.run(); } } 

I am able to achieve the goal with the above when I run it from Eclipse IDE. How do I run this from a maven command line?

Snippet of POM.xml -

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.14.1</version> <configuration> <include>com/shn/test/*Tests.class</include> <suiteXmlFiles> <!-- <suiteXmlFile>src/test/resources/TestNG.xml</suiteXmlFile> --> <suiteXmlFile>${tests}</suiteXmlFile> </suiteXmlFiles> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin> 

Currently to execute any given XML I use -

mvn -Dtests=AllTests.xml test 
1
  • I found great guidance on running parallel tests here (which works in maven too): books.google.com/… Commented Jul 31, 2014 at 15:49

4 Answers 4

1

The simplest solution to run tests in parallel is to use the configuration for the maven-surefire-plugin like this:

</plugins> [...] <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.15</version> <configuration> <parallel>methods</parallel> <threadCount>10</threadCount> </configuration> </plugin> [...] </plugins> 

Usually you don't need a separate testng.xml file to run the tests, cause they will be run by default based on the naming conventions for tests. Furthermore it's not needed to make separate include rules apart from that your given definition is wrong.

You can control which tests will run in relationship with TestNG via the groups parameter like this:

mvn -Dgroups=Group1 test 

Furthermore it's possible to control which tests will run via the test property like this:

mvn -Dtest=MyTest test 

or

mvn -Dtest=MyTest,FirstTest,SecondTest test 

A more fine granular way to specify tests from command line is like this:

mvn -Dtest=MyTest#myMethod test 

which run the method myMethod in the MyTest class.

Sign up to request clarification or add additional context in comments.

2 Comments

Each of my suites contain 80-100 tests & I have 8 such suites. It would not be feasible for me to run tests individually with mvn -Dtest=MyTest#myMethod
Which version of maven-surefire-plugin do you use?
0

This is what worked for me:

  1. Go to the directory where you have your pom file.
  2. Then type in: a) mvn test -U -Pselenium-tests (If you want to run tests) b) mvn clean install -Pselenium-tests (If you want to build and run tests)

This will run the test suite(xml file) that you have specified in your pom:

<suiteXmlFiles> <!-- <suiteXmlFile>src/test/resources/TestNG.xml</suiteXmlFile> --> <suiteXmlFile>${tests}</suiteXmlFile> </suiteXmlFiles> 

Comments

0

To run your RunSuitesInParallel class with maven you need use the exec plugin since it's a java class with a main method:

mvn exec:java -Dexec.mainClass="your.package.RunSuitesInParallel" 

If I was in your place I would change the 2 in testng.setSuiteThreadPoolSize(2); to args[0] and use -Dexec.args="2"

Comments

0

You can use wildcards to match large sets.

mvn -Dtest=TestSquare,TestCi*le test 

Surefire Plugin

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.