Running TestNG programmatically using the maven command line

I need to run multiple test suites in parallel. One approach is to create a set file, as shown 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 shown 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 can achieve the goal using the above when I launch it from the Eclipse IDE. How to run this from maven command line?

Snippet from 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 
+4
source share
4 answers

The simplest solution for running tests in parallel is to use the configuration for maven-surefire-plugin as follows:

 </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 do not need a separate testng.xml file to run the tests, because they will be executed by default based on the naming conventions for the tests . In addition, you do not need to make separate inclusion rules, except that your definition is incorrect.

You can control which tests will run according to TestNG using the groups parameter as follows:

 mvn -Dgroups=Group1 test 

In addition, you can control which tests will be executed through the test property as follows:

 mvn -Dtest=MyTest test 

or

 mvn -Dtest=MyTest,FirstTest,SecondTest test 

A finer granular way to specify tests from the command line is as follows:

 mvn -Dtest=MyTest#myMethod test 

which run the myMethod method in the MyTest class.

+1
source

This is what worked for me:

  • Change to the directory where you have the pom file.
  • Then enter: a) mvn test -U -Pselenium-tests (if you want to run tests) b) mvn clean install -Pselenium-tests (if you want to create and run tests)

This will launch the test suite (xml file) that you specified in your folder:

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

To run your RunSuitesInParallel class using maven, you need to use the exec plugin , since it is a java class with the main method:

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

If I were you, I would change 2 to testng.setSuiteThreadPoolSize(2); on args[0] and used -Dexec.args="2"

0
source

You can use wildcards to fit large sets.

 mvn -Dtest=TestSquare,TestCi*le test 

Surefire Plugin

0
source

Source: https://habr.com/ru/post/1489499/


All Articles