How to Run Specific TestNG Suite with Maven from Command Line?


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.

In this article we will illustrate how to run specific TestNG suite with maven from command line.

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, run it using command line as mvn test −DxmlFilePath=<testng.xml filepath>.

Example

The following code to show how to run only 1 test method from a large suite:

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>

Command to Run specific suite

Please note if multiple suits are required to exclude simply write all suits name separated by commas

mvn 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

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements