How to run a Particular TestNG Suite from Multiple Suites using Maven in Jenkins?


TestNG is a testing framework and can use Maven as build tool. It helps to maintain dependencies and their version at one place in pom.xml

Maven provides flexibility to run using surefire plugin. If a user has multiple testng.xml files (please note one testng files contains only one test suite), he/she may run the specific suite based on the requirement. Maven provides the flexibility to define suiteXMLFiles as variable and pass the value of variable at run time using command line.

Jenkins is a CI/CD tool and same flexibility it provides us to run specific testng.xml from multiple suites.

In this tutorial, we will illustrate how to run specific TestNG suite using maven in Jenkins.

Approach/Algorithm to solve this problem

  • Step 1: Create a TestNG classes − NewTestngClass

  • Step 2: Write 2 @Test method in the class.

  • Step 3: Now create the testNG.xml as given

  • Step 4: Add the properties in pom.xml as given below:

<properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <suiteFile></suiteFile>
</properties>
  • Step 5: Add suiteXMLFiles tag in pom.xml and mention a variable name as ${ suiteFile} for testng.xml file path as shown in program section. It should be same as mentioned in properties.

  • Step 6: Now, set up the build with Jenkins and go to Build −> Goals and options

  • Step 7: Pass the value as

    clean test −DsuiteFile=src/test/main/testng.xml It will run mentioned testng.xml

Example

The following code to show how to run only 1 test method from a large suite:

import org.testng.annotations.Test;

public class NewTestngClass {

    @Test()
    public void testCase1() {
        System.out.println("in test case 1 of NewTestngClass");
    }
   @Test(groups = { "group2", "group3" })
    public void testCase2() {
        System.out.println("in test case 2 of NewTestngClass");
	   
    } 
}  

pom.xml

This is a maven configuration file that is used to organize dependencies, plugins and run the TestNG test cases.

It is very handy when limited tests are needed to execute rather than full suite.

<?xml version="1.0" encoding="UTF-8"?>
<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.sample</groupId>
    <artifactId>TestNGProjectct</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                  <suiteXmlFiles>                        <suiteXmlFile>${suiteFile}</suiteXmlFile>
                    </suiteXmlFiles>  
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <suiteFile></suiteFile>
    </properties>
    <dependencies>          
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.3.0</version>
        </dependency>
    </dependencies>
</project>

Command to Run Specific Suite

Please note if multiple suits are required to exclude simply write all suits name separated by commas

clean test -DsuiteFile=src/main/java/testng.xml

Output

[INFO] Running TestSuite
...
... TestNG 7.3.0 by Cédric Beust (cedric@beust.com)
...

in test case 1 of NewTestngClass 
in test case 2 of NewTestngClass 
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.314 s 

Updated on: 18-Aug-2023

409 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements