How to run a TestNG class without using testng.xml with maven?
Last Updated :
10 Sep, 2024
Running a TestNG class without using testng.xml
in a Maven project lets you directly execute specific test classes from the command line. By using the Maven Surefire Plugin, you can easily specify which test class to run without using the testng.xml file.
How maven helps in running a TestNG class without testng.xml
Maven helps in running TestNG tests without the need for a testng.xml file by using the Surefire Plugin. The Surefire Plugin is a key component in Maven's build lifecycle for testing. It allows you to specify which test classes to run directly through command-line options like -Dtest, bypassing the need to define your tests in a testng.xml file. This flexibility simplifies the process of running individual tests or groups of tests without modifying configuration files. The Surefire Plugin automatically discovers and runs tests in your project. It supports various testing frameworks, including TestNG and JUnit.
Steps to run a TestNG class without using testng.xml with maven?
Following are the steps to run a TestNG class without using testng.xml with maven.
Step 1: Create a TestNG Class
First, create a simple TestNG class.
Example.java
Java
package com.geeksforgeeks.mavenProject;
import org.testng.annotations.*;
public class Example {
@Test
public void testMethod1() {
System.out.println("This is test method 1.");
}
@Test
public void testMethod2() {
System.out.println("This is test method 2.");
}
}
Step 2: Set Up Maven Project
In this step make sure that your Maven project has the necessary dependencies in the pom.xml. you can configure the maven-surefire-plugin in your pom.xml to specify the test class you want to run.
Your POM.xml file should look like this.
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.geeksforgeeks</groupId>
<artifactId>mavenProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mavenProject</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Step 3: Running the TestNG Class Without testng.xml
You can run the TestNG class directly from the command line without needing to create a testng.xml file.
- Open Terminal in the Eclipse
- Write the following command
mvn test
Output
outputConclusion
With the help of above example we can understand how to run TestNG tests in a Maven project without the need for a testng.xml file. Maven Surefire Plugin provides a powerful and flexible way to run TestNG tests without the need for a testng.xml file. We can either specify the test classes directly via the command line or configure them in the pom.xml file.