Introduction
TestNG is an open-source testing framework used to automate tests. It is an enhanced version of JUnit and NUnit. NG stands for Next Generation in TestNG. It makes end-to-end testing easy and generates accurate and elaborated test reports. TestNG is distributed under the Apache Software license.
Selenium does not have the feature to generate proper tests and reports alone. It needs support from any framework or third-party tools to achieve this. So TestNG is the best to use with selenium rather than other frameworks as it provides various features. It uses annotations and groups like all the other testing frameworks to represent tests and their behavior. Let’s learn about groups and the features further in this article.
TestNG groups
When we have a lot of test cases to run in an application, it is complex to always run them individually(especially if they have similar functionality). Running these tests individually consumes time, memory, and energy. So to overcome this, TestNG provides us with a feature called “groups”. We can group the test cases with similar functionality and execute them by running a single command, even if they belong to the different classes. It saves us from defining multiple classes within the same test file. The tests are grouped in the .xml file. Let’s look at the syntax given below to understand how to group tests.
@Test (groups = { "front-end", "API" })
In the above example, we grouped the front-end and API test cases and defined them as tests with the @Test annotation.
There are some cases where we grouped a few test cases, but we do not need them anymore, or we need to include one or two tests into a group. TestNG provides us with include and exclude tags to include or remove the test cases from the group. Let’s learn about these tags with examples.
Include
We use the <include> tag inside a TestNG group to include the necessary test cases. We can include one test at a time in the XML file using the include tag. Let’s learn how to include tests from the example given below.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="testSuiteExample" >
<test name="testExample" >
<groups>
<run>
<include name = "testMethod">
</include>
</run>
</groups>
<classes>
<class name="TestNGExample" />
</classes>
</test>
</suite>
We created a test suite named “testSuiteExample” in the above code with a group of tests. It includes the test method named “testMethod” inside the groups tag, so this method will also run along with all the other methods in the class TestNGExample.
Exclude
We use the <exclude> tag inside a TestNG group to exclude the test cases that are not required. We can exclude one test at a time in the XML file using the <exclude> tag. Let’s learn how to exclude tests from the example given below.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="testSuiteExample" >
<test name="testExample" >
<groups>
<run>
<exclude name = "testMethod">
</exclude>
</run>
</groups>
<classes>
<class name="TestNGExample" />
</classes>
</test>
</suite>
We created a test suite named “testSuiteExample” in the above code with a group of tests. It excludes the test method named “testMethod” inside the groups tag, so this method will be excluded and stopped from execution inside the class TestNGExample.