jeffsia
jeffsia

Reputation: 369

How to run testng.xml from Maven command line

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

Upvotes: 15

Views: 81878

Answers (5)

ravi ch
ravi ch

Reputation: 31

Followed exactly same as solution provided by @S-Kerrigan.But it is behaving differently in below scenario.

In my folder, there are 3 testng xml files but i am passing only one testng xml file during run time but it is executing all three xml files..

In properties in pom file defined as below:

<defaultSuiteFiles>sampletestng</defaultSuiteFiles>
<suiteFile>${defaultSuiteFiles}</suiteFile> 

In plugin as below:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven-surefire-plugin.version}</version>
     <configuration>
      <testFailureIgnore>true</testFailureIgnore>
        <suiteXmlFiles>
         <suiteXmlFile>${suiteFile}</suiteXmlFile>
        </suiteXmlFiles>
    </configuration>
</plugin>

In CMD mvn clean test -DdefaultSuiteFiles=src/test/resources/TestSuites/precondiiton.xml

Upvotes: 0

Vivek Singh
Vivek Singh

Reputation: 1025

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

Upvotes: 1

Prakash Palnati
Prakash Palnati

Reputation: 3409

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

Upvotes: 32

niharika_neo
niharika_neo

Reputation: 8531

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.

Upvotes: 5

S-Kerrigan
S-Kerrigan

Reputation: 89

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.

Upvotes: 5

Related Questions