Group Test in TestNG
TestNG allows test methods to be categorized into groups using the groups attribute in @Test. When a group is executed, all methods belonging to that group run.
Defining groups:
Java
public class GroupTestExample {
@Test(groups = { "sanity" })
public void loginByEmail() {
System.out.println("this is login by email");
}
@Test(groups = { "sanity" })
public void loginByFacebook() {
System.out.println("this is login by facebook");
}
@Test(groups = { "sanity", "regression" })
public void signupByEmail() {
System.out.println("signup by email");
}
@Test(groups = { "sanity", "regression" })
public void signupByFacebook() {
System.out.println("signup by facebbok");
}
@Test(groups = { "regression" })
public void paymentReturnByBank() {
System.out.println("payment return by bank");
}
@Test(groups = { "regression" })
public void paymentInDollar() {
System.out.println("this is payment by dollar method");
}
@Test(groups = { "regression" })
public void paymentInRupees() {
System.out.println("this is payment by rupees method");
}
}
Running specific groups via testng.xml:
XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Sample Suite">
<test name="testing">
<groups>
<run>
<include name="regression"/>
<include name="sanity" />
</run>
</groups>
<classes>
<class name="GroupingTests.GroupTestExample" />
</classes>
</test>
</suite>
Run only sanity tests:
XML
<groups>
<run>
<include name="sanity"/>
</run>
</groups>
Exclude a group:
XML
<groups>
<run>
<include name="regression"/>
<exclude name="sanity"/>
</run>
</groups>
Common group naming conventions:
- ✓
smoke/sanity— quick, critical path tests (~5 minutes) - ✓
regression— full regression suite - ✓
critical— must-pass tests - ✓
flaky— known unstable tests to exclude
Method-level group annotation:
Java
@Test(groups = { "sanity", "regression" })
// This test runs when EITHER sanity OR regression group is selected
Key points:
- ✓A test method can belong to multiple groups
- ✓Groups are specified in testng.xml
<include>and<exclude>tags - ✓Execute a group:
@Test(groups={"aaa"})+ testng.xml with<include name="aaa"/>
