Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running TestNG programmatically using maven command line

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
like image 744
praneel Avatar asked Jul 03 '13 13:07

praneel


1 Answers

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.

like image 152
khmarbaise Avatar answered Nov 09 '22 23:11

khmarbaise