How to Run a testng.xml File From Batch for a Maven Project?


TestNG is a testing framework and can use Maven as build tool. It helps to maintain dependencies and their version at one place in pom.xml

Maven provides flexibility to run using surefire plugin. If a user has multiple testng.xml files (please note one testng files contains only one test suite), he/she may run the specific suite based on the requirement. Maven provides the flexibility to define suiteXMLFiles as variable and pass the value of variable at run time using command line. And, at the same time user can create batch file using this command and run it as batch file.

In this article we will illustrate how to run specific TestNG suite with maven using batch file.

Approach/Algorithm to solve this problem

  • Step 1: Create a TestNG classes − NewTestngClass

  • Step 2: Write 2 @Test method in the class.

  • Step 3: Now create the testNG.xml as given

  • Step 4: Add suiteXMLFiles tag in pom.xml and mention a variable name as ${xmlFilePath} for testng.xml file path as shown in program section.

  • Step 5: Now, create a batch file using command line as mvn clean test −DxmlFilePath=<testng.xml filepath>.

  • Run the batch file.

Example

src/ NewTestngClass.java

import org.testng.annotations.Test;

public class NewTestngClass {

    @Test()
    public void testCase1() {
        System.out.println("in test case 1 of NewTestngClass");
    }
   @Test(groups = { "group2", "group3" })
    public void testCase2() {
        System.out.println("in test case 2 of NewTestngClass");
	   
    }
}  

pom.xml

This is a maven configuration file that is used to organize dependencies, plugins and run the TestNG test cases.

It is very handy when limited tests are needed to execute rather than full suite.

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sample</groupId>
    <artifactId>TestNGProjectct</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                  <suiteXmlFiles>                        <suiteXmlFile>${xmlFilePath}</suiteXmlFile>
                    </suiteXmlFiles>  
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>          
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.3.0</version>
        </dependency>
    </dependencies>
</project>

Batch file

Write the following command and save as .bat file

set projectLocation=C:\locationOfYourProject\locationOfYourTestNGXmlFile
cd %projectLocation%
mvn clean test -DxmlFilePath=src/main/java/testng.xml

Output

[INFO] Running TestSuite
...
... TestNG 7.3.0 by Cédric Beust (cedric@beust.com)
...

in test case 1 of NewTestngClass 
in test case 2 of NewTestngClass 
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.314 s 

Updated on: 18-Aug-2023

805 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements