Grouping Test cases in TestNG

TestNG provides an excellent feature of grouping test cases. Let us consider a scenario where we have 5 features to test and in the project, we have 5 Java Classes, one for each feature. and we want to execute all the sanity scenarios of all these 5 features. This can be handled using Groups attribute with @Test annotation.

Let us learn Grouping with an example. Consider below two classes TestClass1 and TestClass2 which have 2 test cases with group = “Sanity” each.

Code of TestClass1:

package com.qatechhub.testng.package1;

import org.testng.annotations.Test;

public class TestClass1 {
	@Test
	public void tc1(){
		System.out.println("Executing testcase1 of testclass1..");
	}
	
	@Test (groups = "Sanity")
	public void tc2(){
		System.out.println("Executing testcase2 of testclass1..");
	}
	
	@Test
	public void tc3(){
		System.out.println("Executing testcase3 of testclass1..");
	}
	
	@Test (groups = "Sanity")
	public void tc4(){
		System.out.println("Executing testcase4 of testclass1..");
	}

}

Code of TestClass2:

package com.qatechhub.testng.package1;

import org.testng.annotations.Test;

public class TestClass2 {
	@Test (groups = "Sanity")
	public void tc1(){
		System.out.println("Executing testcase1 of testclass2..");
	}
	
	@Test
	public void tc2(){
		System.out.println("Executing testcase2 of testclass2..");
	}
	
	@Test (groups = "Sanity")
	public void tc3(){
		System.out.println("Executing testcase3 of testclass2..");
	}
	
	@Test
	public void tc4(){
		System.out.println("Executing testcase4 of testclass2..");
	}

}

Now to execute only “Sanity” test cases of both these classes we will write a testng.xml file, we can include and exclude any group in an xml file.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Package Execution Suite">
  <test name="Package Execution">
  <groups>
  <run>
  	<include name="Sanity"></include>
  </run>
  </groups>
  <classes>
  	<class name="com.qatechhub.testng.package1.TestClass1"></class>
  	<class name="com.qatechhub.testng.package1.TestClass2"></class>
  </classes>
  </test>
</suite>

Result after running above testng.xml file:

[TestNG] Running:
C:\workspace\LearningSelenium\src\com\qatechhub\testng\package1\testng.xml

Executing testcase2 of testclass1..
Executing testcase4 of testclass1..
Executing testcase1 of testclass2..
Executing testcase3 of testclass2..

Here, only Sanity Test cases of both the classes are executed.

Excluding a Group from Execution:

We can also exclude a group from execution. Say for example in above scenario, if we want to skip all Sanity Test cases. we can use below testng.xml file for the same.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Package Execution Suite">
  <test name="Package Execution">
  <groups>
  <run>
  	<exclude name="Sanity"></exclude>
  </run>
  </groups>
  <classes>
  	<class name="com.qatechhub.testng.package1.TestClass1"></class>
  	<class name="com.qatechhub.testng.package1.TestClass2"></class>
  </classes>
  </test>
</suite>

After execution result is as below:

[TestNG] Running:
C:\workspace\LearningSelenium\src\com\qatechhub\testng\package1\testng.xml

Executing testcase1 of testclass1..
Executing testcase3 of testclass1..
Executing testcase2 of testclass2..
Executing testcase4 of testclass2..

Multiple Group Execution:

Multiple groups can be included or excluded from a run. See below code:

package com.qatechhub.testng.package1;

import org.testng.annotations.Test;

public class TestClass1 {
	@Test
	public void tc1(){
		System.out.println("Executing testcase1 of testclass1..");
	}
	
	@Test (groups = {"Sanity", "Regression"})
	public void tc2(){
		System.out.println("Executing testcase2 of testclass1..");
	}
	
	@Test
	public void tc3(){
		System.out.println("Executing testcase3 of testclass1..");
	}
	
	@Test (groups = "Sanity")
	public void tc4(){
		System.out.println("Executing testcase4 of testclass1..");
	}

}

Here, in above code tc2() method is a part of both “Sanity” and “Regression” group and it will be executed if any of the group is executed.

Multiple Groups can be included or excluded from testng.xml file. See below code:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Package Execution Suite">
  <test name="Package Execution">
  <groups>
  <run>
  	<exclude name="Sanity"></exclude>
  	<exclude name="Regression"></exclude>
  </run>
  </groups>
  <classes>
  	<class name="com.qatechhub.testng.package1.TestClass1"></class>
  	<class name="com.qatechhub.testng.package1.TestClass2"></class>
  </classes>
  </test>
</suite>

When we add a Groups attribute to @Test annotation same attribute with same value is required in all @Before**** or @After**** annotations to execute these methods or pass an attribute alwaysRun = true in case you want before and after method to execute always irrespective of the group.

PS: For any questions queries or comment feel free to write us at saurabh@qatechhub.com or support@qatechhub.com. Happy Learning

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: