Table of contents
1.
Introduction
2.
TestNG groups
2.1.
Include
2.2.
Exclude
3.
Frequently Asked Questions
4.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

TestNG Groups

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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" })
You can also try this code with Online Java Compiler
Run Code


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.

Frequently Asked Questions

  1. What is the TestNG framework?
    TestNG is an open-source testing framework used to automate tests. It makes end-to-end testing easy and generates accurate and elaborated test reports. It can be used to support other frameworks or IDEs to generate reports.
     
  2. Why do we group the test cases in TestNG?
    We group the test cases in TestNG with similar functionality to avoid running the similar cases multiple times and wasting our time. When we run similar tests multiple times, they consume a lot of memory, which might become complex to maintain in the future.
     
  3. Why do we need to integrate TestNG with selenium?
    TestNG makes end-to-end testing easy and generates accurate and elaborated test reports. It supports selenium to generate test reports as selenium does not have the feature within it.
     
  4. What does an include tag do?
    The <inlude> tag includes the tests into a group that are not a part of them. This allows us to perform efficient testing and saves our memory and time by executing tests to validate a logic at a time.
     
  5. What are the different ways to exclude a test in TestNG?
    We can exclude a test by using the <exclude> tag inside the groups, so the excluded test will no longer be executed. We can also “enabled” attribute to false with the @Test annotation to exclude a specific test.

Key Takeaways

We have discussed the TestNG framework and its groups in this article. Now you can perform tests using the annotations discussed in this article.

Hey Ninjas! We hope this blog helped you understand the concept of the TestNG framework better. If you want to learn more, check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems. Please upvote our blog to help the other ninjas grow.

Happy Coding!

Live masterclass