TestNG Groups in Selenium Java – Complete Guide
By Bhau Automation • Learn how to group and run test methods efficiently
🎯 What You Will Learn in This Tutorial
- What are TestNG groups and why they are useful
- How to group test methods from different classes
- Running Smoke, Sanity, and Regression tests using groups
- Using
and tags in testng.xml - BeforeGroups and AfterGroups annotations
- Running multiple suites and specifying groups
- Real-world examples and best practices
💡 Pro Tip: Grouping tests reduces execution time and allows better organization for large test suites.
📌 What Are TestNG Groups?
TestNG Groups allow you to categorize your test methods and selectively run them. This is particularly useful when you have multiple test classes and want to run a specific set of tests like smoke or regression tests.
- Run specific tests from multiple classes
- Organize tests into logical groups (e.g., Smoke, Regression, Sanity)
- Enable selective execution in CI/CD pipelines
🔧 How to Define and Use TestNG Groups
Here’s an example of grouping test methods in Selenium:
import org.testng.annotations.Test;
public class LoginTest {
@Test(groups = {"smoke","regression"})
public void loginValidUser() {
System.out.println("Login test for valid user");
}
@Test(groups = {"regression"})
public void loginInvalidUser() {
System.out.println("Login test for invalid user");
}
}
public class SearchTest {
@Test(groups = {"smoke"})
public void searchProduct() {
System.out.println("Search product test");
}
@Test(groups = {"regression"})
public void searchEmptyCart() {
System.out.println("Search with empty cart test");
}
}
📋 Running Groups from testng.xml
You can include or exclude groups in your testng.xml file:
🔄 Using BeforeGroups and AfterGroups
TestNG allows setup and teardown specifically for groups:
import org.testng.annotations.*;
public class GroupSetup {
@BeforeGroups("smoke")
public void beforeSmokeGroup() {
System.out.println("Setup for Smoke tests");
}
@AfterGroups("smoke")
public void afterSmokeGroup() {
System.out.println("Cleanup after Smoke tests");
}
}
🎯 Benefits of Using TestNG Groups
- Run only required tests to save execution time
- Organize large test suites logically
- Enable targeted execution in CI/CD pipelines
- Combine multiple classes in a single group
❓ Common Interview Questions
Q: How do you group test cases in TestNG?
A: Use the groups attribute in @Test annotations and configure include/exclude in testng.xml.
Q: Can a test method belong to multiple groups?
A: Yes, a test method can belong to multiple groups by providing an array of group names.
🎥 Watch the Complete Video Tutorial
👉 Watch on YouTube: TestNG Groups in Selenium Java
🎓 Key Takeaways
- TestNG groups allow selective and efficient execution of tests
- Include and exclude groups in testng.xml for better control
- Use BeforeGroups and AfterGroups for group-specific setup/teardown
- Grouping helps in organizing large suites and improving CI/CD pipelines
⚡ Next Steps: Practice creating groups for your own test suites and run selective tests using testng.xml.
🚀 Created with ❤️ by Bhau Automation