TestNG Groups
What Are TestNG Groups?
TestNG groups allow you to categorize test methods into logical collections and then selectively run or skip them. You assign a method to one or more groups via the groups attribute on @Test:
@Test(groups = {“smoke”})
public void smokeTest() { … }
@Test(groups = {“regression”,”fast”})
public void fastRegressionTest() { … }
Group of Groups (Meta-Groups)
Sometimes you want to treat several groups as a single unit. TestNG lets you define a “meta-group” in testng.xml that aggregates other groups:
<groups>
<!– Define a meta-group named “allTests” –>
<define name=”allTests”>
<include name=”smoke”/>
<include name=”regression”/>
</define>
<run>
<!– Run everything in “allTests” –>
<include name=”allTests”/>
</run>
</groups>
Here, allTests will encompass both smoke and regression methods
Including and Excluding Groups
Within your <test> in testng.xml, you can specify exactly which groups to run or skip:
<suite name=”SuiteWithGroups”>
<test name=”SelectiveTests”>
<groups>
<run>
<!– Only run smoke tests –>
<include name=”smoke”/>
<!– Skip any slow tests –>
<exclude name=”slow”/>
</run>
</groups>
<classes>
<class name=”com.example.tests.MyTests”/>
</classes>
</test>
</suite>
- <include> — only these groups run
- <exclude> — these groups are skipped
Regular Expressions and TestNG Groups
TestNG also supports regex patterns in your <include> or <exclude> names, enabling wildcard selections:
<groups>
<run>
<!– Include any group whose name starts with “reg” –>
<include name=”reg.*”/>
<!– Exclude any group ending in “_beta” –>
<exclude name=”.*_beta”/>
</run>
</groups>
This runs all groups like regression, regioTest, etc., while skipping v2_beta, feature_beta, and so on.
Putting It All Together
A full testng.xml combining these features might look like:
<!DOCTYPE suite SYSTEM “https://testng.org/testng-1.0.dtd”>
<suite name=”AdvancedGroupSuite”>
<parameter name=”env” value=”staging”/>
<test name=”GroupedRun”>
<groups>
<define name=”coreTests”>
<include name=”smoke”/>
<include name=”fast.*”/>
</define>
<run>
<include name=”coreTests”/>
<exclude name=”.*_deprecated”/>
</run>
</groups>
<classes>
<class name=”com.example.tests.MyTests”/>
</classes>
</test>
</suite>
With this you get:
- Meta-grouping (via <define>) to bundle related groups
- Selective execution (via <include>/<exclude>)
- Pattern matching (via regex) for flexible group names
Together, these capabilities give you powerful, maintainable control over exactly which tests run in each scenario.