🧩 What Is Test Grouping?
In TestNG, you can assign one or more groups to your test methods using the groups attribute in the @Test annotation.
This allows you to:
Run specific categories of tests (e.g., “smoke”, “regression”).
Skip or include only certain types of tests.
Control execution easily through the testng.xml configuration file.
🧱 1. Basic Example — Grouping Tests
import org.testng.annotations.Test;
public class GroupExample {
@Test(groups = { "smoke" })
public void loginTest() {
System.out.println("Smoke Test - Login");
}
@Test(groups = { "regression" })
public void addProductTest() {
System.out.println("Regression Test - Add Product");
}
@Test(groups = { "smoke", "regression" })
public void logoutTest() {
System.out.println("Smoke & Regression Test - Logout");
}
}
✅ Here:
loginTest() runs in the smoke group.
addProductTest() runs in the regression group.
logoutTest() runs in both groups.
⚙️ 2. Running Groups via testng.xml
You can include or exclude groups inside your TestNG suite XML configuration.
🧩 Example: Include Only “smoke” Tests
<suite name="GroupedSuite">
<test name="SmokeTests">
<groups>
<run>
<include name="smoke" />
</run>
</groups>
<classes>
<class name="tests.GroupExample" />
</classes>
</test>
</suite>
✅ Only loginTest() and logoutTest() will run.
🧩 Example: Exclude “regression” Tests
<suite name="ExcludeRegressionSuite">
<test name="WithoutRegression">
<groups>
<run>
<exclude name="regression" />
</run>
</groups>
<classes>
<class name="tests.GroupExample" />
</classes>
</test>
</suite>
✅ All tests except those in the regression group will execute.
🔁 3. Multiple Groups in One Test
You can assign multiple groups to the same method:
@Test(groups = { "smoke", "ui" })
public void homePageTest() {
System.out.println("Smoke + UI test");
}
You can also combine multiple includes in your XML:
<include name="smoke" />
<include name="ui" />
🧩 4. Grouping Across Classes
You can group tests that are spread across different classes.
// File: LoginTests.java
@Test(groups = "smoke")
public void validLoginTest() { ... }
// File: ProductTests.java
@Test(groups = "regression")
public void addToCartTest() { ... }
Then in testng.xml:
<suite name="EcommerceSuite">
<test name="SmokeOnly">
<groups>
<run>
<include name="smoke" />
</run>
</groups>
<classes>
<class name="tests.LoginTests" />
<class name="tests.ProductTests" />
</classes>
</test>
</suite>
✅ Only smoke tests will execute, even if other tests exist in those classes.
⚡ 5. Nested / Meta Groups
You can define group dependencies so that one group automatically includes another.
Example:
@Test(groups = "database")
public void dbConnectionTest() { ... }
@Test(groups = "api", dependsOnGroups = "database")
public void apiFetchTest() { ... }
✅ Here, the api group will only run after the database group finishes successfully.
🧠 6. Running Groups from Command Line
You can also specify groups directly without XML:
# Run only smoke tests
java -cp "bin;libs/*" org.testng.TestNG -groups smoke testng.xml
# Exclude regression tests
java -cp "bin;libs/*" org.testng.TestNG -excludegroups regression testng.xml
✅ 7. Summary Table
Feature Description Example
Group a test Categorize tests logically @Test(groups = "smoke")
Multiple groups Belong to several groups @Test(groups = {"smoke", "regression"})
Include groups Run only selected groups <include name="smoke"/>
Exclude groups Skip certain groups <exclude name="regression"/>
Group dependency Run groups in a sequence dependsOnGroups
💡 Example: Real-World Use Case
@Test(groups = "smoke")
public void openHomePage() { ... }
@Test(groups = "sanity")
public void checkLoginPage() { ... }
@Test(groups = "regression")
public void verifyUserCanAddItemsToCart() { ... }
You can now:
Run quick smoke tests before a release.
Run regression tests for full coverage.
Combine both groups when needed.
Learn Selenium with JAVA Training in Hyderabad
Read More
Introduction to TestNG Annotations
How to Use Assertions in Selenium Tests
Working with Links and Buttons in Selenium
Handling Checkboxes and Radio Buttons in Selenium
Visit Our Quality Thought Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments