HOME
In the previous tutorial, we have discussed about How to create dependency between Selenium Test Cases using TestNG. In this tutorial, we will see how we can group test cases using TestNG. TestNG allows us to group test methods which is not present in JUnit. We can declare the methods belong to groups as well as specify groups that contain other groups. To group test cases, we need to create testng.xml.
Multiple tags are used in a sequence to build a working testNG xml like , and
1. First is tag, which holds a logical name which defines full information to TestNG reported to generate execution report.
2. Second is , which is a logical name which holds the information of test execution report like pass, fail, skip test cases and other information like total time for execution and group info
3. Third is , where TestNGDemo is the package used, and Test Class name is TestNGGroupDemo.
4. Fourth is , where “e2etest” is the name of the method we want to execute.
<?xml version = "1.0"encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name = "Suite1">
<test name = "Test Demo">
<groups>
<run>
<include name = "e2etest"/>
</run>
</groups>
<classes>
<class name = "TestNGGroupDemo"/>
</classes>
</test>
</suite>
To Run the TestNG program, right click on the testng.xml and select Run As-> TestNG Suite (in case of Eclipse) and Run testng.xml (in case of IntelliJ).

In below example, we have shown the syntax of how to use groups in the XML file. @Test(groups = { “e2etest”, “integerationtest” }) .
Below is the sample code.
import org.testng.annotations.Test;
public class TestNGGroupDemo {
@Test(alwaysRun = true, groups = { "e2etest", "integerationtest" })
public void testPrintMessage() {
System.out.println("This method is run by both e2e and integeration test");
}
@Test(alwaysRun = true, groups = { "e2etest" })
public void testE2EMessage() {
System.out.println("This method is run by e2e test");
}
@Test(alwaysRun = true, groups = { "integerationtest" })
public void testingIntegrationMessage() {
System.out.println("This method is run by integeration test");
}
@Test(alwaysRun = true, groups = { "acceptancetest" })
public void testingAcceptanceMessage() {
System.out.println("This method is run by Acceptance test");
}
@Test(alwaysRun = true, groups = { "e2etest", "acceptancetest" })
public void testE2EAndAcceptanceMessage() {
System.out.println("This method is run by both e2e and acceptance test");
}
@Test(alwaysRun = true, groups = { "e2etest", "integerationtest", "acceptancetest" })
public void testE2EAndAcceptanceAndIntegrationMessage() {
System.out.println("This method is run by e2e, integration and acceptance test");
}
}
Output
[RemoteTestNG] detected TestNG version 7.4.0
This method is run by e2e, integration and acceptance test
This method is run by both e2e and acceptance test
This method is run by e2e test
This method is run by both e2e and integeration test
===============================================
Suite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================
The output of the above program is

The result will look like something shown below. Here, we can see that Test Case Passed is 4, Failed 0 and Skipped 0.
To view the report, go to the Eclipse folder and you can see a folder with name test-output inside the Project where we have created TestNG class. Here, it is C:\Users\vibha\eclipse-workspace\Demo\test-output

Groups of Groups
Groups can also include other groups. These groups are called “MetaGroups”. TestNG provides the flexibility of providing groups inside another group and running them according to your needs.
Let’s create a group inside a group in our XML file.
Here, I have created a created a new group with the name ‘SuperGroup‘ and included our group “acceptancetest” into it. Then we called the newly created group (SuperGroup) for execution by including it in the run tag. The output will be like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name = "Test Demo">
<groups>
<define name = "SuperGroup">
<include name = "acceptancetest"></include>
</define>
<run>
<include name = "SuperGroup"/>
</run>
</groups>
<classes>
<class name="TestNGGroupDemo"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
The output of the above program is

Lets, add another group “e2etest” to SuperGroup. In this case, we will see all the tests marked with “e2etest” and “acceptancetest“.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name = "Test Demo">
<groups>
<define name = "SuperGroup">
<include name = "acceptancetest"></include>
<include name = "e2etest"></include>
</define>
<run>
<include name = "SuperGroup"/>
</run>
</groups>
<classes>
<class name="TestNGGroupDemo"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
The output of the above program is

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!