TestNG – How to group Tests in Selenium using TestNG

HOME

<?xml version = "1.0"encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name = "Suite1">
  <test name = "Test Demo">
      <groups>
         <run>
              <include name = "e2etest"/>
          </run>
        </groups>
    <classes>
          <class name = "TestNGGroupDemo"/>
     </classes>  
   </test>
</suite>

In below example, we have shown the syntax of how to use groups in the XML file. @Test(groups = { “e2etest”, “integerationtest” }) .

Below is the sample code.

import org.testng.annotations.Test;

public class TestNGGroupDemo {

	@Test(alwaysRun = true, groups = { "e2etest", "integerationtest" })
	public void testPrintMessage() {
		System.out.println("This method is run by both e2e and integeration test");
	}

	@Test(alwaysRun = true, groups = { "e2etest" })
	public void testE2EMessage() {
		System.out.println("This method is run by e2e test");
	}

	@Test(alwaysRun = true, groups = { "integerationtest" })
	public void testingIntegrationMessage() {
		System.out.println("This method is run by integeration test");
	}

	@Test(alwaysRun = true, groups = { "acceptancetest" })
	public void testingAcceptanceMessage() {
		System.out.println("This method is run by Acceptance test");
	}

	@Test(alwaysRun = true, groups = { "e2etest", "acceptancetest" })
	public void testE2EAndAcceptanceMessage() {
		System.out.println("This method is run by both e2e and acceptance test");
	}

	@Test(alwaysRun = true, groups = { "e2etest", "integerationtest", "acceptancetest" })
	public void testE2EAndAcceptanceAndIntegrationMessage() {
		System.out.println("This method is run by e2e, integration and acceptance test");
	}

}

Output
[RemoteTestNG] detected TestNG version 7.4.0
This method is run by e2e, integration and acceptance test
This method is run by both e2e and acceptance test
This method is run by e2e test
This method is run by both e2e and integeration test

===============================================
Suite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================

The output of the above program is

The result will look like something shown below. Here, we can see that Test Case Passed is 4, Failed 0 and Skipped 0.

To view the report, go to the Eclipse folder and you can see a folder with name test-output inside the Project where we have created TestNG class. Here, it is  C:\Users\vibha\eclipse-workspace\Demo\test-output

Groups of Groups

Groups can also include other groups. These groups are called “MetaGroups”. TestNG provides the flexibility of providing groups inside another group and running them according to your needs.
Let’s create a group inside a group in our XML file.

Here, I have created a created a new group with the name ‘SuperGroup‘ and included our group “acceptancetest” into it. Then we called the newly created group (SuperGroup) for execution by including it in the run tag. The output will be like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
   <test name = "Test Demo">
       <groups>
        	<define name = "SuperGroup">
   			   <include name = "acceptancetest"></include>
   		</define>
          <run>
              <include name = "SuperGroup"/>
          </run>
        </groups>
    <classes>
      <class name="TestNGGroupDemo"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Lets, add another group “e2etest” to SuperGroup. In this case, we will see all the tests marked with “e2etest” and “acceptancetest“.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
   <test name = "Test Demo">
       <groups>
        	<define name = "SuperGroup">
   			   <include name = "acceptancetest"></include>
   			    <include name = "e2etest"></include>
   		</define>
          <run>
              <include name = "SuperGroup"/>
          </run>
        </groups>
    <classes>
      <class name="TestNGGroupDemo"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Leave a comment