How to Run Specific TestNG Suite with Maven from Command Line?
Last Updated :
18 Sep, 2024
When using Maven as your build automation tool and TestNG for Java-based testing, you may need to execute particular test suites instead of running every test. Running test subsets according to functionality, environment, or any other criteria can be helpful in this regard. You must set up your Maven project and run the tests in a focused way in order to accomplish this.
How to Run Specific TestNG Suite with Maven from the Command Line.
Following are the steps to run specific testNG suite with Maven from Command Line
Step 1: Create a Maven Project
Create a maven project on any IDE, like Eclipse.
Step 2: Create a TestNG Class
Now, you need to create a simple TestNG class. This class will contain the test methods you want to execute.
ExampleTest.java
Java
package com.example;
import org.testng.annotations.Test;
public class ExampleTest {
@Test
public void testMethodOne() {
System.out.println("Test Method One is running");
}
@Test
public void testMethodTwo() {
System.out.println("Test Method Two is running");
}
}
Step 3: Configure pom.xml
In this step, modify the pom.xml
file to include the TestNG dependency and the Maven Surefire plugin for running TestNG tests.
pom.xml
XML
<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.example</groupId>
<artifactId>TestNGMavenProject</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- TestNG Dependency -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step 4: Create the testng.xml
File
Define the TestNG suite by creating the testng.xml
file in the src/test/resources
directory. This file will specify which tests to run.
testng.xml
Java
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="ExampleSuite">
<test name="ExampleTest">
<classes>
<class name="com.example.ExampleTest"/>
</classes>
</test>
</suite>
Step 5: Run the TestNG Suite from the Command Line
Finally, run the TestNG suite using Maven from the command line. This command executes the tests defined in the testng.xml
file.
mvn clean test -DsuiteXmlFile=src/test/resources/testng.xml
Output
OutputConclusion
Running a specific TestNG suite with Maven from the command line allows you to efficiently manage and execute targeted subsets of your test cases. This process is crucial for maintaining a streamlined and effective testing workflow, especially in large projects or continuous integration (CI) environments.