Grouping of Test Cases in TestNG

In the previous post, we have seen how to prioritize the test cases in TestNG. In this post, we will learn about Grouping of Test Cases in TestNG.

In TestNG , we have one more annotation known as “Groups” which can be used to executed multiple test cases belong to the same group. Suppose, you have 100 test cases related to “Bank”. Now, out of 100, 50 are related to HDFCBank, 20 are related to ICICIBank, 10 are related to SBIBank and so on. There might be requirement that you have to execute test cases only related to HDFCBank. This can be possible by using annotation “Groups“.

How to implement it ?

For understanding its implementation, let’s create the 2 groups HDFCBank and SBIBank. A bank can have different branches in other areas but each branck will belong to the same bank. Likewise, we have create some test as shown below. Branch1 and Branch2 belong to the group HDFCBank , Branch3 and Branch4 belong to group SBIBank. We have one more method Branch5  which belongs to 2 groups “HDFCBank” and “Bank“.

Syntax: (groups = { ” Group Name” })

import org.testng.annotations.Test;

public class GroupExample {

	@Test(groups={"HDFCBank"})
	public void Branch1(){
		System.out.println("Test 1 : HDFC Branch");
	}
	
	@Test(groups={"HDFCBank"})
	public void Branch2(){
		System.out.println("Test 2 : HDFC Branch");
	}
	
	@Test(groups={"SBIBank"})
	public void Branch3(){
		System.out.println("Test 3 : SBI Branch");
	}
	
	@Test(groups={"SBIBank"})
	public void Branch4(){
		System.out.println("Test 4 : SBI Branch");
	}
	
	@Test(groups={"HDFCBank","Bank"})
	public void Branch5(){
		System.out.println("Test 4 : HDFC Branch and Main Bank");
	}
}

testng.xml: 

<suite name="ExampleTestSuite" >
  <test name="Bank functionality">
  <groups>
  	<run>
  	  <include name="HDFCBank"/>
  	</run>
  </groups>
    <classes>
      <class name="com.testsuite.example.GroupExample"/>
    </classes>
  </test>
</suite>

Right click on testng.xml and run it as testng suite.

Output in console:

Group run in testng

Result of running suite: 

Group testng result

 

As you can see that we have included only HDFCBank group in testng.xml , so it has executed only those test cases Branch1 and Branch2. Branch5 is also executed, because it also has group “HDFCBank” along with another group “Bank”.

Now, if we want to run all the groups. Then , we can add all the groups in testng.xml.

<suite name="ExampleTestSuite" >
  <test name="Bank functionality">
  <groups>
  	<run>
  	  <include name="HDFCBank"/>
  	  <include name="SBIBank"/>
  	</run>
  </groups>
    <classes>
      <class name="com.testsuite.example.GroupExample"/>
    </classes>
  </test>
</suite>

Run the suite and you will get the result as below. This time all the test cases will be executed.

Run all group in TestNG


 

This is box title
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...