Home » TestNG Groups

TestNG Groups

TestNG Groups allow you to perform groupings of different test methods. Grouping of test methods is required when you want to access the test methods of different classes.

Not only you can declare the methods within a specified group, you can also declare another group within a specified group. Thus, TestNG can be asked to include a certain set of groups while excluding another set of groups.

It provides you maximum flexibility by partitioning your test methods in groups and does not require recompilation of test cases if you run your two different sets of test cases back to back.

Groups are specified in the testng.xml file with <groups> tag. Groups can be specified either in the <suite> tag or <test> tag. If the <groups> tag is specified inside the <suite> tag, then it is applied to all the <test> tags of XML file. If the <groups> tag is specified within a particular <test> folder, then it is applied to that particular <test> tag only.

Let’s understand the concept of TestNG Groups through an example:

Test cases within Groups

First case: When <groups> tag is defined inside the <suite> tag.

Step 1: Open the Eclipse.

Step 2: We create three java projects, i.e., Personal_loan.java, Home_loan.java, and Car_loan.java.

Personal_loan.java

Home_loan.java

Car_loan.java

In the above case, we provide a group name, i.e., SmokeTest to three test cases of three different classes.

Step 3: Now, we create a testng.xml file where we configure the classes that we have created and add new tag <groups>. We want to execute those test cases which have a group “SmokeTest”.

Output

TestNG Groups

Second case: When <groups> tag is defined inside the tag.

The testng.xml would look like:

Tests belonging to multiple Groups

Step 1: Open the Eclipse.

Step 2: We create a java project named as “Groups.java”.

Groups.java

In the above code, we define two groups, i.e., Group A and Group B. The testcase1() is tagged with a ‘Group A’, testcase2 is tagged with two groups ‘Group A’ and ‘Group B’, and testcase3() is tagged with a ‘Group B’.

Step 3: We create the testng.xml file to configure the Groups class.

testng.xml file

Step 4: Run the testng.xml file by clicking right click on the testng.xml file.

Output

TestNG Groups

Including/Excluding Groups

Step 1: Open the Eclipse.

Step 2: We create a new java project.

Groups.java

Step 3: We will create the testng.xml file.

testng.xml file

Step 4: Run the testng.xml file.

Output

TestNG Groups

Using Regular Expressions

We can also use regular expressions with TestNG Groups.

Let’s understand through an example:

Step 1: Open the Eclipse.

Step 2: We create a java project named as “Regular_Expression.java”.

Regular_Expression.java

Step 3: Now we create the testng.xml file to configure the above class.

Step 4: Run the testng.xml file.

Output

TestNG Groups

Groups in Groups

We can also specify a group within another group. The groups which are defined in another groups are known as Meta Groups.

Let’s understand through an example:

Step 1: Open the Eclipse.

Step 2: We create a java project named as “Groups_in_Groups“.

Groups_in_Groups.java

Step 3: Now we create a testng.xml file where we configure the above class.

testng.xml file

In the above xml file, we define a new group within another group named as “Group 1” and we have include those test cases which are tagged with “Smoke” and “Regression”.

Step 4: Run the testng.xml file.

Output

TestNG Groups


Next TopicTestNG Annotations

You may also like