One of the coolest features in TestNG is the ability to group test methods. By grouping tests, you can separate different sections or features of a project you are working on. It also enables extending to the dependency feature so that a particular group of tests are executed only if one group or method has passed.
Tests can belong to a single or multiple groups. And again, you can have a group of different groups called MetaGroups. To use this feature, you have to provide the group name(s) as an array in the group attribute:
@Test(groups = { "Group-A" })Now let’s look at a code example.
Tests in Single or Multiple Groups
The below example shows three groups that belong to at least one group.
File: GroupDemo.java
package TestNGDemo;
import org.testng.annotations.Test;
public class GroupDemo {
@Test(groups = { "Group-A" })
public void testA() {
System.out.println("I belong to Group A");
}
@Test(groups = { "Group-A", "Group-B" })
public void testB() {
System.out.println("I belong to Group A and Group B");
}
@Test(groups = { "Group-B" })
public void testC() {
System.out.println("I belong to Group B");
}
}testA() and testC() belong to “Group-A” and “Group-B,” respectively, while testB() is a member of both “Group-A” and “Group-B.”

Run a TestNG Group using testng.xml
Executing this XML file as a TestNG suite will run both A and B groups:
File: testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Group Demo">
<test name="Group A">
<groups>
<run>
<include name="Group-A" />
</run>
</groups>
<classes>
<class name="TestNGDemo.GroupDemo" />
</classes>
</test>
<test name="Group B">
<groups>
<run>
<include name="Group-B" />
</run>
</groups>
<classes>
<class name="TestNGDemo.GroupDemo" />
</classes>
</test>
</suite>
Group TestNG Class
Just like methods, Classes can also be grouped in TestNG. In below example, every public method of the class “GroupDemo” belong to group “Group-A”.
File: GroupDemo.java
package TestNGDemo;
import org.testng.annotations.Test;
@Test(groups = { "Group-A" })
public class GroupDemo {
public void testA() {
System.out.println("I belong to Group A");
}
public void testB() {
System.out.println("I belong to Group A");
}
}
Exclude a Group from Running
Using <exclude> tag we can prevent a particular group of tests from running:
File: testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Exclude/Include Group Demo">
<test name="Group Demo">
<groups>
<run>
<include name="Group-A" />
<exclude name="Group-B" />
</run>
</groups>
<classes>
<class name="TestNGDemo.GroupDemo" />
</classes>
</test>
</suite>
MetaGroup: Create groups of groups
Using TestNG XML file, we can create a group of groups called MetaGroup.
File: testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MetaGroup">
<test name="MetaGroup Demo">
<groups>
<define name="all-groups">
<include name="Group-A" />
<include name="Group-B" />
</define>
<run>
<include name="all-groups" />
</run>
</groups>
<classes>
<class name="TestNGDemo.GroupDemo" />
</classes>
</test>
</suite>
In the above example, we have created a new group called “all-groups” with the methods of Group-A and Group-B. Calling the new MetaGroup, we can execute all the tests inside the sub groups.