Grouping of Test methods in TestNG

TestNG allows users to perform different types of grouping to its test methods. We can't only declare to which group the respective methods belong, but also we can specify groups that contain other groups. As well as we can include a certain set of them via regular expressions and also exclude them.

Groups can be found either under the <test> tag or <suite> tag. The groups mentioned under the <suite> apply to all the <test> tags mentioned below.

NOTE: If we specify a group named 'loginTest' in <suite> and 'logoutTest' in <test. Then, both of them will be included.

Let's check these with a few examples -

public class TC {
@Test (groups = { "loginTest", "functionalityTest" })
public void testMethod1(){ }

@Test (groups = { "loginTest", "functionalityTest" })
public void testMethod2(){ }

@Test (groups = { "loginTest" })
public void testMethod3(){ }
}

Executing via TestNG :

<test name="Test1">
<groups>
<run> <include name="functionalitytest"/>
</run>
</groups>
<classes>
<class name="example1.Test1"/>
</classes>
</test>

Here, testMethod1() and testMethod2() are invoked.

Excluding or Including Method Groups

Again considering the same groups (loginTest and functionalityTest). We can use in the below format.

public class TC {
@Test (groups = { "functionalityTest" })
public void testMethod1(){ }

@Test (groups = { "loginTest" })
public void testMethod3(){ }
}

TestNG.xml can be written as -

<test name="Test1">
<groups>
<run> <include name="functionalitytest"/>
<exclude name="loginTest"/>
</run>
</groups>
<classes>
<class name="example1.Test1"/>
</classes>
</test>

These can also be used with Regular Expressions.

Usage of Regular Expressions in TestNG

Let's consider a different scenario here, as per the requirements we've to verify a few functionalities only on Chrome Browser and a few on Safari along with Firefox. Then we can customize our tests in the below way -

public class TC {
@Test (groups = { "chrome.loginTest", "chrome.functionalityTest" })
public void testMethod1(){ }

@Test (groups = { "firefox.loginTest", "firefox.functionalityTest" })
public void testMethod2(){ }

@Test (groups = { "safari.loginTest" })
public void testMethod3(){ }
}

Executing via TestNG :

<test name="Test1">
<groups>
<run> <include name = "chrome.*", "safari.*"/>
</run>
</groups>
<classes>
<class name="example1.Test1"/>
</classes>
</test>

Here, testMethod1() and testMethod3() are executed.

Groups of groups in TestNG

It means TestNG has the feasibility to execute methods that are groups of another group. Also referred as 'MetaGroups'. These are to be declared under the <groups> tag.

<test name="Test1">
<groups>

<define name = "chromeTests">
<include name = "chrome.*"/>
</define>

<define name = "firefoxTests">
<include name = "firefox.*"/>
</define>

<define name = "safariTests">
<include name = "safari.*"/>
</define>

<run>
<include name = "chrome.*", "safari.*"/>
</run>

</groups>

<classes>
<class name="example1.Test1"/>
</classes>
</test>

Here, the tests which include chrome.* and safari.* are executed!