How to run testng.xml from Maven command line

51,499

Solution 1

Easiest solution would be, add the below lines in surefire plugin.

<suiteXmlFiles>
    <!-- pass testng.xml files as argument from command line -->
    <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
</suiteXmlFiles>

And run your xml file as below,

mvn clean test -Dsurefire.suiteXmlFiles=/path/to/testng.xml

Solution 2

If you have a fixed testng xml, then you just need to do mvn clean test. The surefire plugin would be executed by default in the test phase of maven and the xml hardcoded would be picked up.

Solution 3

in first, please add improvement in pom.xml... need adding execution section:

<executions>
<execution>
    <phase>test</phase>
    <goals>
        <goal>test</goal>
    </goals>
</execution>
</executions>

In second, you can create section in pom.xml with variables. And, then, calling it from cmd.

<properties>
<defaultSuiteFiles>
        ./Sets/Suits/CMS_Administration_SiteBackup_029.xml,
    </defaultSuiteFiles>
    <suiteFile>${defaultSuiteFiles}</suiteFile>
</defaultSuiteFiles>
</properties>
...
...
<suiteXmlFiles>
    <suiteXmlFile>${suiteFile}</suiteXmlFile>
</suiteXmlFiles>

So, result of prototype pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
.....
.....


<properties>        
    <defaultSuiteFiles>
        ./Sets/Suits/CMS_Administration_SiteBackup_029.xml,
    </defaultSuiteFiles>
    <suiteFile>${defaultSuiteFiles}</suiteFile>


    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<profiles>
    <profile>
        <id>Base configuration</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <defaultGoal>install</defaultGoal>
            <plugins>                    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.19.1</version>
                    <inherited>true</inherited>
                    <executions>
                        <execution>
                            <phase>test</phase>
                            <goals>
                                <goal>test</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <suiteXmlFiles>
                            <suiteXmlFile>${suiteFile}</suiteXmlFile>
                        </suiteXmlFiles>                            
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.3.1</version>
    </dependency>
</dependencies>
</project>

And use this, how a example for cmd: mvn test -DdefaultSuiteFiles="./YOUR_DIRECTORY/YOUR_SUITE.xml,"

Explanations: in pom.xml you setup xmls for default and place to variable. And, if you will use variable in cmd, you will rewrite values.

Solution 4

Need to pass path from the content root of testng.xml file between suiteXmlFiles tag

for e.g. for my project in place of testng.xml i am using file name as AllTests.xml, which is located under folder TestSuites

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>src/test/resources/TestSuites/AllTests.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
 </plugin>

and now run mvn clean test, it will pick the test suite file from pom.xml and run all the test classes mentioned in this suite.

Note:

  1. Reason we need to mention complete path of testng.xml file is, because by default maven will search this file directly under the project level

  2. If you using IntelliJ you can find Path from content root for your testng.xml file, by doing right click on the file-> Copy Path -> Path from content root

Share:
51,499
jeffsia
Author by

jeffsia

Updated on June 09, 2021

Comments

  • jeffsia
    jeffsia almost 3 years

    I have the following entry in my pom.xml with the suite file defined in the configuration.

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
            <suiteXmlFiles>
                <suiteXmlFile>testng.xml</suiteXmlFile>
            </suiteXmlFiles>
        </configuration>
    </plugin>
    

    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
    
  • jeffsia
    jeffsia about 7 years
    Tried this. It works. Seems to be the simplest way to trigger the testng xml. Thank you!
  • Abhishek
    Abhishek over 3 years
    Thank you prakash. This has helped me a lot. In most of the threads answer is wrong but your's has helped a lot.
  • QOPS
    QOPS about 3 years
    After lot of browsing, this finally worked. Thanks Prakash.
  • vaibhavcool20
    vaibhavcool20 over 2 years
    I am getting this error [ERROR] Unknown lifecycle phase ".suiteXmlFile=./testng.xml".
  • vaibhavcool20
    vaibhavcool20 over 2 years
    never-mind i was using powershell earlier. command prompt is fine.