
🏹Introduction to TestNG Groups
TestNG Groups: TestNG is a powerful testing framework that supports various types of tests, such as unit, functional, UI, integration, and end-to-end testing. One of its most useful features is TestNG Groups, which allows testers to group multiple test methods logically and control their execution efficiently.
Using TestNG Groups, you can:
✔ Run specific test cases based on functional grouping
✔ Exclude unwanted test cases from execution
✔ Manage large test suites effectively
✔ Execute selective test cases using XML configuration
In this tutorial, we will explore how to use TestNG Groups, including how to include and exclude test cases in XML configuration.
🏹Why Use TestNG Groups?
TestNG Groups:
There are situations where you may not want to run all test cases together. Instead, you might want to:
✔ Run specific test methods based on functionality
✔ Exclude test cases dynamically without modifying the code
✔ Execute test cases based on priorities
To achieve this, TestNG provides a grouping mechanism using the @Test annotation.
🏹How to Use TestNG Groups in Java
In TestNG, you can assign test cases to multiple groups using the groups attribute in the @Test annotation.
Example: Defining TestNG Groups in Java
java
import org.testng.annotations.Test;
public class TestGroupsExample {
@Test(groups = {“bonding”})
public void testMethod1() {
System.out.println(“Test Method 1 – Bonding Group”);
}
@Test(groups = {“strong_ties”})
public void testMethod2() {
System.out.println(“Test Method 2 – Strong Ties Group”);
}
@Test(groups = {“bonding”, “strong_ties”})
public void testMethod3() {
System.out.println(“Test Method 3 – Both Groups”);
}
}
Explanation:
✔ testMethod1() belongs to the bonding group.
✔ testMethod2() belongs to the strong_ties group.
✔ testMethod3() belongs to both bonding and strong_ties groups.
🏹How to Include & Exclude Groups in TestNG XML
Instead of modifying the Java code, TestNG allows us to include or exclude test groups directly in the testng.xml file.
Example: XML File for Including Specific Groups
xml
<suite name=”TestNG Suite”>
<test name=”Group Testing”>
<groups>
<run>
<include name=”bonding”/>
</run>
</groups>
<classes>
<class name=”TestGroupsExample”/>
</classes>
</test>
</suite>
✔ Only test cases in the “bonding” group will execute.
🏹Example: XML File for Excluding a Group
xml
<suite name=”TestNG Suite”>
<test name=”Group Testing”>
<groups>
<run>
<exclude name=”strong_ties”/>
</run>
</groups>
<classes>
<class name=”TestGroupsExample”/>
</classes>
</test>
</suite>
✔ The “strong_ties” group will be excluded, meaning all other test cases will execute except those in this group.
🏹How to Run TestNG XML in Eclipse
- Create a new TestNG project in Eclipse.
- Write your test cases and define groups in Java.
- Create the testng.xml file and configure include/exclude settings.
- Run the testng.xml file by right-clicking it and selecting Run As → TestNG Suite.
🏹Test Scenario: Using Groups with Include/Exclude Mechanism
Let’s consider a practical example where we test the login functionality of a banking application.
Test Scenario Steps:
1️⃣ Launch the website
2️⃣ Verify login page heading
3️⃣ Enter username and password
4️⃣ Verify the dashboard
5️⃣ Verify links on the dashboard
Java Code Implementation:
java
public class BankTestCases {
@Test(groups = {“bonding”})
import org.testng.annotations.Test;
public void tc01_LaunchURL() {
System.out.println(“Launching Bank URL…”);
}
@Test(groups = {“bonding”})
public void tc02_VerifyLoginPage() {
System.out.println(“Verifying Login Page…”);
}
@Test(groups = {“bonding”, “strong_ties”})
public void tc03_EnterCredentials() {
System.out.println(“Entering Credentials…”);
}
@Test(groups = {“strong_ties”})
public void tc04_VerifyDashboard() {
System.out.println(“Verifying User Dashboard…”);
}
@Test(groups = {“bonding”})
public void tc05_VerifyHyperlinks() {
System.out.println(“Checking Dashboard Links…”);
}
}
XML Configuration for Excluding a Test Case
xml
<suite name=”Banking Test Suite”>
<test name=”Login Test”>
<groups>
<run>
<exclude name=”strong_ties”/>
</run>
</groups>
<classes>
<class name=”BankTestCases”/>
</classes>
</test>
</suite>
✔ This will execute only the “bonding” group test cases and exclude all “strong_ties” test cases.
🏹Scenarios for Running Test Cases Using Groups
Scenario 1: Run All Test Cases
- Remove the <groups> tag from the XML file to execute all test cases.
Scenario 2: Run Only Specific Groups
- Use the <include> tag in testng.xml to specify the groups you want to execute.
Scenario 3: Exclude Specific Test Cases
- Use the <exclude> tag to skip particular test cases.
Scenario 4: Selectively Include Test Cases
- Use multiple <include> tags to run only specific test cases.
🏹Advantages of Using TestNG Groups
✅ Efficient Test Execution: Run only relevant test cases instead of executing the entire suite.
✅ Better Test Case Organization: Easily manage and categorize test cases.
✅ Time-Saving: Avoid running unnecessary test cases.
✅ Flexible XML Configuration: No need to modify the code; just update the XML file.
✅ Parallel Execution Support: TestNG allows you to run grouped tests in parallel for faster execution.
🏹Conclusion
TestNG Groups provide a powerful and flexible way to manage test execution by categorizing test cases into groups. By using include and exclude mechanisms in XML, testers can execute only the necessary test cases, saving time and improving efficiency.