How We Can Run Testng.xml File on Terminal with Maven?

I am going to show you how we can run testng.xml file on terminal. Sometimes we need to run dozens or hundreds of test cases. We can run them all with one line command.

I have three java class under my project and I want to run all of them.

We have to add java classes that we want to run to testng.xml file. There is a detail of testng.xml file. I added ParallelTesting, FirstChromeSearch and MultipleBrowser. Last class, under the package that’s why I added this class with its package’s name. You can add as many cases as you want.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
<test verbose="2" preserve-order="true" name="Command Line Tests" >
<classes>
<class name="ParallelTesting"></class>
<class name="FirstChromeSearch"></class>
<class name="MultipleBrowser.MultipleBrowser"></class>
</classes>
</test>
</suite>

We need to add plugin to pom.xml file.

<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>

Now we open terminal and go to testng.xml file’s path and run this command.

mvn clean test -DsuiteXmlFile=testng.xml

Now you can see test results on terminal. How many tests are fail or how many tests are success?

Thank you for reading.

Quality Assurance Engineer-Automation