How to Run Specific TestNG Test Group via Maven?
Last Updated :
10 Sep, 2024
TestNG allows you to group your tests and execute specific groups selectively. By integrating TestNG with Maven, you can easily run these specific test groups through a simple Maven command. This article will guide you through setting up and running specific TestNG test groups via Maven.
What is TestNG Test Group?
TestNG test groups allow you to organize your tests into categories. This is useful when you want to run a subset of tests based on certain criteria, like sanity tests, regression tests, or smoke tests.
How to Group Tests in TestNG?
In TestNG, you can group tests using the @Test annotation with the `groups` attribute.
The below code snippet is an example:
Java
import org.testng.annotations.Test;
public class GroupedTest {
@Test(groups = {"sanity"})
public void testMethodOne() {
System.out.println("This is a sanity test");
}
@Test(groups = {"regression"})
public void testMethodTwo() {
System.out.println("This is a regression test");
}
@Test(groups = {"sanity", "regression"})
public void testMethodThree() {
System.out.println("This is both a sanity and regression test");
}
}
How to Run Specific TestNG Test Group via Maven
Step 1: Install JDK
To execute Java programs, we need Java Development Kit (JDK). So you need to Download and Install JDK on your system.
Step 2. Install Eclipse IDE
You can use any Java IDE (like IntelliJ IDEA), but for this tutorial we are using Eclipse IDE. So you need to install Eclipse IDE in your system.
Step 3. Create a Maven Project in Eclipse
After Eclipse is installed, open it and create a new Maven project. Choose "pom.xml" from the left-hand side menu.
Step 4. Configure dependencies
After creating a new project, we need to add dependencies such as Selenium and TestNG in the `pom.xml` file.
Add the following code into `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>TestNGGroupsExample</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- TestNG Dependency -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
<scope>test</scope>
</dependency>
<!-- Selenium Dependency -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.17.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
NOTE: Always update the version of the dependencies.
Step 5. Create a Java Test Case with Selenium TestNG
First we will create a Java class within `src > test > java directory`, as in the below image:
Step 6: Configure testng.xml to Include Specific Groups
In your `testng.xml` file, you can specify which groups you want to include or exclude during the test run.
Add the following code in `testng.xml`:
XML
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestNG Groups Example">
<test name="Sanity Test">
<groups>
<run>
<include name="sanity"/>
</run>
</groups>
<classes>
<class name="GroupedTest"/>
</classes>
</test>
</suite>
NOTE: In this example, only tests belonging to the "sanity" group will be executed.
Step 6. Run the Test Case
To run a specific test group, use the following Maven command:
mvn test -Dgroups="sanity"
Output
OutputConclusion
Running specific TestNG test groups via Maven is a powerful way to control which tests are executed during your build process. This approach allows you to focus on different aspects of your application, like sanity checks or regression testing, without running the entire test suite.
Similar Reads
How to Run Specific TestNG Suite with Maven from Command Line?
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 Mave
2 min read
How to skip TestNG test at runtime?
With TestNG, there are situations where some tests are needed to be skipped during runtime based on certain conditions. Dynamic skipping of tests provides more flexibility while executing the test at run-time, depending upon some external factor like configuration, system states, or the outcome of s
3 min read
How to Create Test Suite in TestNG?
In this article we will learn about test suites and how can we build them using Java and TestNG. TestNG is a modern testing framework that is used very widely used today. It has a broad base of features that allow us to write unit tests and club them together in groups. Table of Content What is a Te
11 min read
How to run single test in a TestNG class from IntelliJ IDE?
TestNG is a widely-used testing framework in the Java programming language. Often, when developing or debugging your code, you may need to run a specific test method rather than executing the entire test suite. Fortunately, IntelliJ IDEA makes this task straightforward with its integrated support fo
3 min read
How to Control Test Execution Order in TestNG?
Controlling the order in which tests run is crucial, especially in larger software projects. By controlling the test execution order, you ensure that tests run logically. This helps in identifying real issues in the code, rather than false negatives caused by running tests out of sequence. It also m
3 min read
How to exclude TestNG Groups from Maven?
To exclude specific TestNG groups from execution when running tests through Maven, you can configure the maven-surefire-plugin in the Maven pom.xml file. The Surefire plugin is responsible for running the unit tests during the build process, and it supports excluding certain groups from being execut
2 min read
How to run multiple test classes in TestNG?
TestNG makes it easy to run multiple test classes in one go, which helps test different parts of an application efficiently. By running multiple classes together, you save time and ensure more thorough testing. You can run these classes using a TestNG XML file, directly from Eclipse, or through the
2 min read
How to run multiple test cases using TestNG Test Suite in Selenium?
In Selenium automation testing, efficiently managing and executing multiple test cases is crucial. TestNG, a powerful Java checking-out framework, offers an excellent answer to this. Using TestNG, you can group tests, create test units, and keep them organized. This article guides you through the st
4 min read
How to stop TestNG from running after a test fail?
In TestNG, a popular testing framework for Java, managing test execution flow effectively is crucial for ensuring the reliability of your test suites. One common requirement is to stop TestNG from running subsequent tests after a failure. This can be particularly useful when a test failure might inv
3 min read
How to retrieve the test suite name at runtime in TestNG?
Sometimes in TestNG it's necessary to retrieve the name of the test suite at runtime. This is especially true when generating reports, doing logging, or debugging. The ITestContext interface provides various methods to get details about the current test run. Approach to Retrieve Test Suite Name at R
2 min read