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
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
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:
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
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With