Open In App

How to exclude TestNG Groups from Maven?

Last Updated : 27 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 executed.

Approach to Exclude TestNG Groups from Maven

Following are the steps to exclude TestNG groups from Maven:

  • Create a Maven Project: First, create a maven project on any ide like Eclipse or IntelliJ
  • Create a class: Create a java class (eg:TestClass1) under src/test/java in that Maven project
  • Add Test Methods: Add Test Methods in each class using @test annotation add a group attribute to each method and write your logic in that test method.
  • Configure POM.xml file: In the pom.xml file, configure the maven-surefire-plugin to exclude specific groups using the <excludedGroups> tag. This tag allows you to specify which groups should be excluded during test execution.
  • Run the project: To run the project. Open the terminal and write the mvn test, this will exclude the group.

Example Code

TestClass1.java

Java
package TestNG.SeleniumJava;
import org.testng.annotations.Test;

public class TestClass1 {
    @Test(groups = "group1")
    public void testMethod1() {
        System.out.println("TestClass1 - testMethod1 from group1");
    }

    @Test(groups = "group2")
    public void testMethod2() {
        System.out.println("TestClass1 - testMethod2 from group2");
    }
}

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>TestNG</groupId>
  <artifactId>SeleniumJava</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SeleniumJava</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
               <configuration>
                <excludedGroups>group2</excludedGroups>
            </configuration>
            </plugin>
        </plugins>
    </build>

  <dependencies>
  <!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.7.1</version>
    <scope>test</scope>
</dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Output:

In order to run the project. Open the terminal and write the following command, this will exclude the group.

mvn test

Conclusion

By configuring the maven-surefire-plugin in the pom.xml file and using the <excludedGroups> tag, you can easily exclude specific TestNG groups from being executed during the Maven build process. This allows for greater flexibility in controlling which tests are run, especially in scenarios where certain tests are not relevant or are under development.


Next Article

Similar Reads