How to group tests in Selenium using TestNG?

Whenever there is any in deployment or release we generally do sanity testing to make sure critical code is working fine as expected. Similarly, we also do regression testing after delivery of new functionality. In general, testers create a separate testng.xml file for each testing activity. This is not a wrong technique, but not an efficient and smart one. TestNG supports grouping of similar tests so that they could be called for the execution from a single testng.xml file. These group tests help in various ways to break down the complex test execution problems.

So, our agenda for this tutorial is to discuss how to group tests in our Selenium project with the help of TestNG file. The goal of delivery managers is to handover the defect-free product to the clients; hence testers play a crucial role to assure the quality of the product. This can only happen when testing processes are efficient and seamless.

Suggested Readings:

How to implement ‘group tests’ feature in the Selenium project using TestNG?

We have already discussed in previous tutorials that grouping tests are one of the properties and added advantage of using TestNG. So, the declaration of grouping is very simple. Here is the group declaration:

Single group name declaration

@Test(groups = {"group1"})

Multiple group name declaration

  @Test(groups = {"group1", "group2"})

Run ‘Group tests’ declaration in testng.xml

<groups>
  	<run>
  	<exclude name="group_name">
  	<include name="group_name">
  	</include>
  	</exclude>
  	</run>
  	</groups>

These are the techniques to declare test groups.

Sample Program

Below is the sample program wherein we have grouped few tests as Smoke while few other as Regression and few as both, Smoke and Regression. We design our testng.xml file in such a way that our requirement to run specified groups get processed.

package Test;

import org.testng.annotations.Test;

public class GroupsSampleProgram {
	
  @Test(groups = {"Sanity"})
  public void sanityTest1() {
	  System.out.println("This is sanity test 1");
  }
  
  @Test(groups = {"Regression"})
  public void regressionTest1() {
	  System.out.println("This is regression test 1");
  }
  
  @Test(groups = {"Sanity"})
  public void sanityTest2() {
	  System.out.println("This is sanity test 2");
  }
  
  @Test(groups = {"Regression"})
  public void regressionTest2() {
	  System.out.println("This is regression test 2");
  }
  
  @Test(groups = {"Sanity"})
  public void sanityTest3() {
	  System.out.println("This is sanity test 3");
  }
  
  @Test(groups = {"Regression"})
  public void regressionTest3() {
	  System.out.println("This is regression test 3");
  }
  
  @Test(groups = {"Sanity"})
  public void sanityTest4() {
	  System.out.println("This is sanity test 4");
  }
  
  @Test(groups = {"Regression"})
  public void regressionTest4() {
	  System.out.println("This is regression test 4");
  }
  
  @Test(groups = {"Sanity", "Regression"})
  public void regressionSanity() {
	  System.out.println("This is regression and Sanity both");
  }
}

Here is the testng.xml file which includes all the groups.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
  	
  	<groups>
  	<run>
  	<include name="Sanity">
  	</include>
  	</run>
  	</groups>
  
    <classes>
      <class name="Test.GroupsSampleProgram"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

We can define one group at a time.

Console output

Below screenshot is the console output generated after running above testng.xml file. It executes all the tests which are having groups=”Sanity” declaration.

One group tests

Let’s look at some other techniques to exclude and include tests to run.

How to include all tests based on group tests declaration?

Let’s look at the changes in testng.xml file which uses <define> </define> to run all the grouped tests. Here is the sample testng.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
  	
  	<groups>
          <define name="all">
             <include name="Sanity"/>
             <include name="Regression"/>
          </define>
         <run>
 <include name="all" />
 </run>        
 </groups>
    <classes>
      <class name="Test.GroupsSampleProgram"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Console Output

Include Group Tests

As per the above screenshot from console we see that all the tests grouped with Sanity and Regression got executed.

How to exclude any test from the group tests?

This sample testng.xml file depicts exclusion of all the tests which contain their group’s definition as Regression. We use <exclude></exclude> inside the testng.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
  	
  	<groups>
  	<run>
  	<exclude name="Regression">
  	<include name="Sanity">
  	</include>
  	</exclude>
  	</run>
  	</groups>
  
    <classes>
      <class name="Test.GroupsSampleProgram"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Console Output

Excluded group tests

 From the above console output, we observe that TestNG excluded all the tests which were having groups=”Regression” in it.

This was all about ‘group tests’ technique in Selenium using TestNG. Stay connected for more knowledge sharing session. Join our Facebook group for more updates.

Join Inviul fb group

Leave a Reply