Open In App

How to Run Specific TestNG Test Group via Maven?

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

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:

Screenshot-2024-08-21-221354

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

Screenshot-2024-08-28-193909
Output

Conclusion

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.


Next Article
Article Tags :

Similar Reads