Groups in TestNG and How to use them

Groups TestNG

In our last tutorial, we learned how to work with TestNG and TestNG annotations. In this tutorial, we will learn how to utilize the potential of TestNG and execute multiple test cases in a group. We can use groups in TestNG to create a collection of test scripts which we want to execute together.

Let’s start by creating two separate classes and adding some tests (methods) to it.

package groups;

import org.testng.annotations.Test;

public class TestGroup01 {
    
    @Test
    public void login() {
        System.out.println("Login Test");
    }
    
    @Test
    public void registrationPage() {
        System.out.println("Registration Test");
    }
    
    @Test
    public void fillForm() {
        System.out.println("Fill Form Test");
    }
    
    @Test
    public void logout() {
        System.out.println("Logout Test");
    }
    

}

Class 1: TestGroup01

package groups;

import org.testng.annotations.Test;

public class TestGroup02 {
    
    @Test
    public void verifyTitle() {
        System.out.println("Verify Title Test");
    }
    
    @Test
    public void verifyLocator() {
        System.out.println("Verify Locator Test");
    }

}

Class 2: TestGroup02

Click HERE to learn how to setup TestNG in eclipse. Once the setup is done, create a package groups and create 2 classes TestGroup01 and TestGroup02 as provided in above code blocks. 

TestGroup01 has 4 methods login(), registrationPage(), fillForm() and logout(). Similarly, TestGroup02 has 2 methods verifyTitle() and verifyLocator(). For the sake of simplicity we are only printing some text for each method, but in real scenario each method denotes a test case.

Why create Groups in TestNG

There could be a situation where you need to verify the functionality on the production environment. As we know, we should never alter any data in production to maintain the data integrity, however, we can execute certain tests on production which would not make any impact.

Looking upon the tests of our current classes, we have a few tests which only verifies certain conditions and do not update any data in production. For eg., we can easily execute login(), logout(), verifyTitle() and verifyLocator() methods as they only fetch the data from the application, whereas registrationPage() and fillForm() would require data update and we should restrain executing these tests on production.

We can group certain test cases which could be executed on the production environment and thus minimize the chance of human error.

Update test class to assign groups

We can assign groups attribute at the test level to categorize different test scripts (tests) which could be later used to execute selected tests only. Update the methods discussed above and assign a group to them. Group name could be anything, we are creating a group prod.

package groups;

import org.testng.annotations.Test;

public class TestGroup01 {
    
    @Test(groups= {"prod"})
    public void login() {
        System.out.println("Login Test");
    }
    
    @Test
    public void registrationPage() {
        System.out.println("Registration Test");
    }
    
    @Test
    public void fillForm() {
        System.out.println("Fill Form Test");
    }
    
    @Test(groups= {"prod"})
    public void logout() {
        System.out.println("Logout Test");
    }
    

}
package groups;

import org.testng.annotations.Test;

public class TestGroup02 {
    
    @Test(groups= {"prod"})
    public void verifyTitle() {
        System.out.println("Verify Title Test");
    }
    
    @Test(groups= {"prod"})
    public void verifyLocator() {
        System.out.println("Verify Locator Test");
    }

}

Modify testng.xml to create groups in TestNG

From the previous tutorial, create a testng.xml file and update it with the following code.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SeleniumTestNG suite">

    <test name="First Group">
        <classes>
            <class name="groups.TestGroup01">

            </class>
        </classes>
    </test>

    <test name="Second Group">
        <classes>
            <class name="groups.TestGroup02">

            </class>
        </classes>
    </test>


</suite>

testng_groups.xml

Above XML is a classic testng.xml template (we have renamed the file name as testng_groups.xml) which contains class name and methods. If you execute this XML file (screenshot below), it should execute all the tests of both the classes. 

Groups in TestNG

Let’s modify this XML file so that certain test scripts (methods) should be executed collectively.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SeleniumTestNG suite">

    <groups>
        <run>
            <include name="prod" />
        </run>
    </groups>

    <test name="First Group">
        <classes>
            <class name="groups.TestGroup01">

            </class>
        </classes>
    </test>

    <test name="Second Group">
        <classes>
            <class name="groups.TestGroup02">

            </class>
        </classes>
    </test>


</suite>

If you execute the above XML, it should execute only 4 test scripts which are assigned the prod groups.

groups in TestNG execution result

groups tag is used before the test name tag (highlighted above in bold) and it accepts the value which we have defined in our test classes. You can assign multiple groups attributes to tests (methods), update the XML and execute them.

Try to assign different groups to remaining 2 tests and execute (after updating the XML) them to see the results.

Author: Dhawal Joshi

A post-graduate in MCA, ISTQB & ITIL certified QA with more than 8 years of experience in QA working with a CMMI Level 5 organization as System Analyst. I started my automation journey with HP UFT(formerly known as QTP) and for the past few years, I am using Selenium for automation. I also have experience in Android Application Development, Java, HTML, and VBScript. When I am not working, I like to spend time with my family, cooking and learning new developments in IT.

Leave a Reply

Your email address will not be published. Required fields are marked *