TestNG Test Group (2024)
In this tutorial, we will learn about the TestNG test group and the TestNG Listeners. This tutorial consists of the points are as follows:
- What is the TestNG test group?
- How to create the TestNG test group?
- Different types of the TestNG test groups.
- Steps to Run the TestNG test group.
TestNG Tutorial :
Q: What is the TestNG test group?
Ans:
In the TestNG, the tests are combined into groups is called the TestNG group. In the TestNG test group, the tester is allowed to select and run the test which one wants to Run or ignore. In TestNG different groups are combined, can use regular expression, etc.
In simple words, the TestNG test group means in the TestNG, a Group which indicates the procedure of grouping different tests together into a simple group, and these tests together are run by just running the group in a single command. Even if the tests belong to different classes does not matter.
Grouping of test methods is allowed by the TestNG.
Important tags used in TestNG Test Groups
<groups>
: In thetestng.xml
file Groups are specified.-
<test>
: If groups are specified inside the suite then it can also be applied to the test. <suite>
: If groups are specified inside the suite then it can be applied to the suite and test both.
Create the Test group
Let's use the groups attribute of @Test
annotation to create a test class that consists of
certain tests that belong to a group name test group.
package com.techgeeknext.testcases;
import org.testng.annotations.Test;
public class FirstTestDemo {
@Test(groups = { "Pre-Group" })
public void testDBConnec() {
}
@Test(groups = { "Pre-Group" })
public void TestLoginUser() {
}
@Test(groups = { "Post-Group" })
public void TestLogoutUser() {
}
@Test
public void SimpleTest() {
}
}
output:
Test Group execution in Eclipse
We will run the group of tests using eclipse.
- Go to Run -> Run Configurations.
- The new configuration window will open. Click on Browse -> Select the Groups -> Run
- Output:
- In Eclipse using TestNG runner configuration successfully implemented the test methods that belonged to a particular group.
- This process can be used for the implementation of multiple groups by selecting the specific groups in the Browse Box.
- Mostly the TestNG XML-based implementation is used for executing the test methods that belong to a specific group.
TestNG Include and Exclude Test Methods
Using include
and exclude
tags defined in testng.xml
, TestNg
allows you to include or exclude Groups,
Test Methods, Classes, and Packages. We will see some examples of how to use include and exclude tags in
a class for Test Methods.
TestNG Include Group from TestNG.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<groups>
<run>
<include name="Pre-Group"></include>
</run>
</groups>
<classes>
<class name="com.techgeeknext.testcases.FirstTestDemo" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
output:
TestNG Exclude Group from TestNG.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<groups>
<run>
<exclude name="Pre-Group"></exclude>
</run>
</groups>
<classes>
<class name="com.techgeeknext.testcases.FirstTestDemo" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
output:
Creating Tests belongs to Multiple Groups in TestNG
TestNG one of the main features is that it allows the test methods to belong to multiple groups.
This
can be implemented by giving the names of the groups as an array attribute of the @Test
annotation.
package com.techgeeknext.testcases;
import org.testng.annotations.Test;
public class FirstTestDemo {
@Test(groups = { "Pre-Group" })
public void testDBConnec() {
}
@Test(groups = { "Pre-Group" })
public void TestLoginUser() {
}
@Test(groups = { "Post-Group" })
public void TestLogoutUser() {
}
@Test(groups = { "Pre-Group", "Post-Group" })
public void SimpleTest() {
}
}
Multiple Groups from testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test-Pre">
<groups>
<run>
<include name="Pre-Group"></include>
</run>
</groups>
<classes>
<class name="com.techgeeknext.testcases.FirstTestDemo" />
</classes>
</test> <!-- Test -->
<test name="Test-Post">
<groups>
<run>
<include name="Post-Group"></include>
</run>
</groups>
<classes>
<class name="com.techgeeknext.testcases.FirstTestDemo" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
The TestNG XML suite consists of two tests, each of them implementing test methods belonging to a particular
group. Select TestNG XML file and run it in the TestNG suite.
output:
Regular Expression in Group Name
TestNG permits the user to use regular expressions while implementing the tests using including, excluding groups method. Include and Exclude groups can be done on the basis of the name search.
package com.techgeeknext.testcases;
import org.testng.annotations.Test;
public class FirstTestDemo {
@Test(groups = { "Pre-Conn-Group" })
public void testDBConnec() {
}
@Test(groups = { "Pre-User-Group" })
public void TestLoginUser() {
}
@Test(groups = { "Post-Group" })
public void TestLogoutUser() {
}
@Test
public void SimpleTest() {
}
}
testng.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<groups>
<run>
<include name="Pre.*"></include>
<exclude name="Post.*"></exclude>
</run>
</groups>
<classes>
<class name="com.techgeeknext.testcases.FirstTestDemo" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
The above XML consists of a simple test in which all the groups with a name starting are included, whereas
all the groups with the name ending are excluded from your test execution.
output:
TestNG Meta Groups
TestNG creates groups out of existing groups and then uses them during the creation of the test suite.
The MetaGroup is created using the tag <define>
inside the tag
<group>
Let's create an update testng.xml and create a group of groups called MetaGroups.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<groups>
<define name="pre-group">
<include name="Pre-Conn-Group" />
<include name="Pre-User-Group" />
</define>
<define name="post-group">
<include name="Post-Group" />
</define>
<run>
<include name="pre-group" />
<exclude name="post-group" />
</run>
</groups>
<classes>
<class name="com.techgeeknext.testcases.FirstTestDemo" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
output: