praneel
praneel

Reputation: 1902

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

Upvotes: 6

Views: 18183

Answers (4)

Aruna
Aruna

Reputation: 701

You can use wildcards to match large sets.

mvn -Dtest=TestSquare,TestCi*le test

Surefire Plugin

Upvotes: 0

Moshisho
Moshisho

Reputation: 2981

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"

Upvotes: 0

sachin jain
sachin jain

Reputation: 224

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>

Upvotes: 0

khmarbaise
khmarbaise

Reputation: 97537

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.

Upvotes: 1

Related Questions