Groups in TestNg selenium with Java

In this tutorial we will learn about the Groups in TestNg in detail and how we can create groups of various test cases.

Groups in TestNg

Grouping is one of the key feature of the TestNg and it is primarily used to create the groups of different test cases. Lets suppose we have the script of n number of test cases out of that some of the test cases belong to one functionality and some of the test cases belong to the some other functionality. So we can make the groups of test cases as per the functionality.

Note : One test case can belong to more than one group also.

Syntax –

@Test(groups={“Regression Testing”} where Regression Testing is a group.
@Test(groups={“Regression Testing” , “System Testing”,”Smoke Testing”}) where Regression Testing,system testing and smoke testing are group.


Example : Lets consider the below example in which there are several test cases and some of the test cases belong to system testing and some of the test cases belong to regression testing and some test cases belong to both and some of the test cases belong to smoke testing.

Test Case –

Test Case 1 ) Open Browser – > common to  system testing and regression Testing
Test Case 2) Open url – > common to  system testing and regression Testing
Test Case 3) login -> common to  system testing and regression Testing
Test Case 4) verify_electronic_items -> Smoke Testing
Test Case 5) verify_phones_items -> Smoke Testing
Test Case 6)verify_shoes_items -> system testing
Test Case 7) verify_clothes_items -> -> regression Testing
Test Case 8) logout – common to  system testing and regression Testing
Test Case 9) close_browser – common to  system testing and regression Testing

Now we have 3 groups –

System testing group which includes Test Case 1, Test Case 2, Test Case 3, Test Case 6 , Test Case 8,Test Case 9
Regression testing group which includes Test Case 1, Test Case 2, Test Case 3, Test Case 7 , Test Case 8,Test Case 9
Smoke testing group which includes Test Case 4 , Test Case 5

Program to execute the Test cases

package com.TestNGTutorial;
import org.testng.annotations.Test;
public class Groupings_Test {

	// method to open the browser
		@Test(groups={"Regression Testing" , "System Testing"})
		public void Testcase1_openbrowser()
		{
			System.out.println("Test Case 1 starts ");
			System.out.println("open the browser - part of all system and regression groups ");
			System.out.println("Test Case 1 ends ");
		}

		// method to open the url
		@Test(groups={"Regression Testing" , "System Testing"})
		public void Testcase2_openurl()
		{
			System.out.println("Test Case 2 Start ");
			System.out.println("open the url - website - part of all system and regression groups ");
			System.out.println("Test Case 2 Ends ");
		}
		
		// method to login into website
		@Test(groups={"Regression Testing" , "System Testing"})
		public void Testcase3_login()
		{
			System.out.println("Test Case 3 starts ");
			System.out.println("login into the website - part of all system and regression groups ");
			System.out.println("Test Case 3 ends ");
		}
		
		// method to verify all the electronic items
		@Test(groups={"Smoke Testing"})
		public void Testcase4_verify_electronic_items()
		{
			System.out.println("Test Case 4 starts ");
			System.out.println("verification of all the electronis items is done"
					+ " - part of Smoke Testing");
			System.out.println("Test Case 4 ends ");
		}

		// method to verify all the electronic items
		@Test(groups={"Smoke Testing"})
		public void Testcase5_verify_phones_items()
		{
			System.out.println("Test Case 5 starts ");
			System.out.println("verification of all the phones items is done"
					+ "-- part of smoke testing groups");
			System.out.println("Test Case 5 ends ");
		}

		// method to verify all the electronic items
		@Test(groups={"System Testing"})
		public void Testcase6_verify_shoes_items()
		{
			System.out.println("Test Case 6 starts ");
			System.out.println("verification of all the shoes items is done"
					+ "- part of system testing");
			System.out.println("Test Case 6 ends ");
		}
		
		// method to verify all the electronic items
		@Test(groups={"Regression Testing"})
		public void Testcase7_verify_clothes_items()
		{
			System.out.println("Test Case 7 starts ");
			System.out.println("verification of all the clothes items is done"
					+ "- part of Regression testing");
			System.out.println("Test Case 7 ends ");
		}
		
		// method to verify all the electronic items
		@Test(groups={"Regression Testing" , "System Testing"})
		public void Testcase8_logout()
		{
			System.out.println("Test Case 8 starts");
			System.out.println("logout is done successfully"
					+ " - part of all system and regression groups ");
			System.out.println("Test Case 8 ends ");
		}
		
		// method to close the browser 
		@Test(groups={"Regression Testing" , "System Testing"})
		public void Testcase9_close_browser()
		{
			System.out.println("Test Case 9 starts ");
			System.out.println("browser is closed"
					+ " - part of all system and regression groups ");
			System.out.println("Test Case 9 ends ");
		}
		
	}

Now executes the System Testing group only – via group tag in testng.xml file

<xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="System Testing begins">  
  <groups><!-- include group here -->
      <run>
        <include name="System Testing"/>        
      </run>
    </groups>
    <classes>      
      <class name="com.TestNGTutorial.Groupings_Test"/>
    </classes>    
  </test> <!-- Test -->
</suite> <!-- Suite -->

Output :  only system testing groups will execute

Test Case 1 starts 
open the browser - part of all system and regression groups 
Test Case 1 ends 
Test Case 2 Start 
open the url - website - part of all system and regression groups 
Test Case 2 Ends 
Test Case 3 starts 
login into the website - part of all system and regression groups 
Test Case 3 ends 
Test Case 6 starts 
verification of all the shoes items is done- part of system testing
Test Case 6 ends 
Test Case 8 starts
logout is done successfully - part of all system and regression groups 
Test Case 8 ends 
Test Case 9 starts 
browser is closed - part of all system and regression groups 
Test Case 9 ends 

===============================================
Suite
Total tests run: 6, Failures: 0, Skips: 0
===============================================

Executing regression test cases

<xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Regression Testing begins">
  <groups>
      <run>
        <include name="Regression Testing"/>
      </run>
    </groups>
    <classes>      
      <class name="com.TestNGTutorial.Groupings_Test"/>
    </classes>    
  </test> <!-- Test -->
</suite> <!-- Suite -->

Output – executes only regression test cases

Test Case 1 starts 
open the browser - part of all system and regression groups 
Test Case 1 ends 
Test Case 2 Start 
open the url - website - part of all system and regression groups 
Test Case 2 Ends 
Test Case 3 starts 
login into the website - part of all system and regression groups 
Test Case 3 ends 
Test Case 7 starts 
verification of all the clothes items is done- part of Regression testing
Test Case 7 ends 
Test Case 8 starts
logout is done successfully - part of all system and regression groups 
Test Case 8 ends 
Test Case 9 starts 
browser is closed - part of all system and regression groups 
Test Case 9 ends 

===============================================
Suite
Total tests run: 6, Failures: 0, Skips: 0
===============================================

Executing smoke test cases

<xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Smoke Testing begins">
  <groups>
      <run>
        <include name="Smoke Testing"/>
      </run>
    </groups>
    <classes>      
      <class name="com.TestNGTutorial.Groupings_Test"/>
    </classes>    
  </test> <!-- Test -->
</suite> <!-- Suite -->

output – only smoke test cases executes

Test Case 4 starts 
verification of all the electronis items is done - part of Smoke Testing
Test Case 4 ends 
Test Case 5 starts 
verification of all the phones items is done-- part of smoke testing groups
Test Case 5 ends 

===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

Groups of Groups in TestNg

Groups in groups in TestNg – we can make groups of groups in TestNg in testng.xml itself.  we can define a separate group in xml file with the below tag.

<define name=”groupsofgroups”><!– define group here –>
<include name=”Regression Testing”/>
<include name=”Smoke Testing”/>
</define><!– define group end here –>

Here name of the group is groupofgroups which includes the regression and smoke testing group

XML to run groups

<xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Smoke Testing begins">  
  <groups><!-- include group here -->
  <define name="groupsofgroups"><!-- define group here -->
            <include name="Regression Testing"/>
            <include name="Smoke Testing"/>
         </define><!-- define group end here -->
      <run>
        <include name="groupsofgroups"/>        
      </run>
    </groups>
    <classes>      
      <class name="com.TestNGTutorial.Groupings_Test"/>
    </classes>    
  </test> <!-- Test -->
</suite> <!-- Suite -->

Output – test case of regression and smoke testing executes

Test Case 1 starts 
open the browser - part of all system and regression groups 
Test Case 1 ends 
Test Case 2 Start 
open the url - website - part of all system and regression groups 
Test Case 2 Ends 
Test Case 3 starts 
login into the website - part of all system and regression groups 
Test Case 3 ends 
Test Case 4 starts 
verification of all the electronis items is done - part of Smoke Testing
Test Case 4 ends 
Test Case 5 starts 
verification of all the phones items is done-- part of smoke testing groups
Test Case 5 ends 
Test Case 7 starts 
verification of all the clothes items is done- part of Regression testing
Test Case 7 ends 
Test Case 8 starts
logout is done successfully - part of all system and regression groups 
Test Case 8 ends 
Test Case 9 starts 
browser is closed - part of all system and regression groups 
Test Case 9 ends 

===============================================
Suite
Total tests run: 8, Failures: 0, Skips: 0
===============================================

This is all about the grouping in TestNg. Hope now you are in a state to answer all the question of Groups in TestNg.