Running TestNG programmatically using maven command line

run testng from command line maven
how to run testng.xml from maven
maven run testng suite
maven command to run test suite
maven surefire plugin example
testng maven
testng reports in maven
how to run testng.xml from command prompt

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

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.

Parametrize to Execute TestNG.xml using Maven, It is like we want to run only the specific tests that are in testng.xml files and Need to pass testng.xml files as parameters from command line� Maven can be used with any of the continuous integration systems. The plugin maven-surefire-plugin is used to configure and execute the tests. Here the said plugin is used to configure the testng.xml and suites-test-testng.xml for the TestNG test and generate test reports.

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"

Maven Surefire Plugin – Using TestNG, Another alternative is to use TestNG suite XML files. This allows flexible configuration of the tests to be run. These files are created in the� What is a the correct way to run this in cmd using maven? I am able to run the test with the command below but it doesn't make sense to indicate the testng.xml again in the command when it's already defined in the pom. mvn clean test -DsuiteXmlFile=testng.xml java maven selenium cmd testng

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>

Running TestNG programmatically - TestNG Beginner's Guide, Running TestNG programmatically. In the earlier chapters we used the testng XML configuration files to configure and define TestNG test suites. The problem with� Goal: To Run TestNG using Command Prompt: Steps to achieve our goal – Executing TestNG using Command Prompt: Open Eclipse and create a Java class; Write a Java program; Convert the Java Program into TestNG; Open command prompt; Run the TestNG using command prompt; Here is a video tutorial to learn “How To Run TestNG Using Command Prompt”:

You can use wildcards to match large sets.

mvn -Dtest=TestSquare,TestCi*le test

Surefire Plugin

Running TestNG Tests Programmatically, Running TestNG tests programmatically will discuss about how to Till now we have executed Duration: 29:57 Posted: Sep 25, 2016 Here is below commands to execute 'testng.xml' file from command line cd F:\Selenium\SampleTestNG java -cp F:\Selenium\SampleTestNG\lib\*;F:\Selenium\SampleTestNG\bin org.testng.TestNG testng.xml After executing above command, it should execute the tests that we have specified in testng.xml file.

Execute TestNG XML Programmatically, If it's a Maven Project, we can run it using mvn test command. Why we need to run testng.xml programmatically? When the public static void main(String[] args ) { TestNG testng = new TestNG(); List<String> suites = Lists. If you are executing any of the above build or package maven goals, you may want to skip tests. JUnit tests written in your maven project can be skipped by adding the maven.test.skip=true option . mvn -Dmaven.test.skip=true package Maven Run Command . Maven's exec plugin can be used to run any of the main class generated in the target folder.

Specifying listeners with testng.xml or in Java, Important: The command line flags that specify what tests should be run will be There are two ways to set these parameters: with testng.xml or programmatically. For e.g, If you were using Maven, you would need to add a dependency as� Command Line Batch File – run it by creating.bat (batch) file Previously we have seen for running a TestNG class using IDE, we need to right-click on the testng.xml file and select run as TestNG which invoke the test class. Now lets us see how we can invoke the testng.xml file from the command line to run the test class in step by step:

Running TestNG Tests from Java Main Method, TestNG tests are executed automatically when we build a maven project. In this case, we can run TestNG test classes from a java main method too. TestNG; public class TestNGMainClass { public static void main(String[] args) { TestNG� As your's is a maven project, you can use the option mvn clean test from command line after navigating to your project path. This will run the tests in the testng.xml file In case if you have multiple test xml files you can use Surefire plugin. To your pom.xml you need to add something like :

Comments
  • I found great guidance on running parallel tests here (which works in maven too): books.google.com/…
  • 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?