How to run testng.xml from Maven command line How to run testng.xml from Maven command line selenium selenium

How to run testng.xml from Maven command line


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


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.


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.