Example of TestNG Groups

0

In this article, I am going to show you an example of TestNG groups.

TestNG groups allow grouping of test methods. In TestNG, a test method can belong to more than one group and likewise, a group can consist of multiple test methods. Once we have the groups formed, we can execute a set of methods via the group or groups they belong to.

It is one of the most important feature of TestNG, you will be able to appreciate its usefulness once I show you an example.

How to group tests?

Suppose for a new feature, my tests are spread across different test classes. Some of the test methods are at unit level, some tests the business scenarios and there are others in the UI layer to do blackbox testing.
As the developer of the new feature, I am only interested in running the tests that directly affect the feature rather than run all the tests as this will give me a quicker feedback of my code. This is where the group concept helps us. Instead of running all the test classes, we can use the TestNG groups feature to group the tests based on the feature they belong to and then run the groups.

So how do we group?
We can group the test methods by specifying the group names they belong to in groups attribute of @Test annotation.

In our example, we have three features, feature1, feature2 and feature3 for which we have written tests at unit level, at business level and UI level. We group each test case based on the feature it belongs. Note that a test case can belong to more than one feature. This is quite common if we have a routine which is used by more than one feature then the unit tests covering the routine will end up belonging to more than one feature.

In the below test class UnitLevelTesting, I have three unit tests. unitTest1 belong to feature1 and feature2 whereas unitTest2 belong to feature2 and unitTest3 belong to feature2. You can see the groups are mentioned in the groups attribute of @Test. If more than one group, you need to specify them comma separated.

UnitLevelTesting:

package com.javarticles.testng;

import org.testng.annotations.Test;

public class UnitLevelTesting {    

    @Test(groups={"feature1","feature2"})
    public void unitTest1() {
        System.out.println("Unit test1");
    }
    
    @Test(groups="feature1")
    public void unitTest2() {
        System.out.println("Unit test2");
    }
    
    @Test(groups="feature2")
    public void unitTest3() {
        System.out.println("Unit test2");
    }
}

If most of the test method belong to the same group or groups, you don’t need to repeat the groups attribute for each test method and instead annotate the class with @Test at the top of a test class, and then use groups attribute in it to specify the groups. For example, in the below test class FunctionalTesting, group feature1 defined at class level will apply to all the public methods of the class.

FunctionalTesting:

package com.javarticles.testng;

import org.testng.annotations.Test;

@Test(groups="feature1")
public class FunctionalTesting {
    @Test
    public void scenario1() {
        System.out.println("Scenario1");
    }
    
    @Test
    public void scenario2() {
        System.out.println("Scenario2");
    }
    
    @Test(groups="feature2")
    public void scenario3() {
        System.out.println("Scenario3");
    }
}

Notice that test method scenario3 too has a groups attribute set to feature2 so is it going to override the groups defined at class level?
No, it is the other way, that is the groups defined at class level will only add up to the method groups, thus it is cumulative.

UITesting:

package com.javarticles.testng;

import org.testng.annotations.Test;

public class UITesting {
    @Test(groups="feature1")
    public void ui1() {
        System.out.println("UI1 testing");
    }
    
    @Test(groups={"feature1", "feature2"})
    public void ui2() {
        System.out.println("UI2 testing");
    }
        
    @Test(groups="feature3")
    public void ui3() {
        System.out.println("UI3 testing");
    }
    
    @Test
    public void ui4() {
        System.out.println("UI3 testing");
    }
}

How to run a TestNG group (or groups)?

We are done with defining the groups. Now its the time to run them.

We can run test methods belonging to a certain group in mainly two ways:

  1. Using the testng XML file.
  2. Through Eclipse

First we will run the tests using testng.xml.

We will modify our test configuration file and add the groups that we want to run.

In the below XML, we add <groups> to instruct TestNG to run all the test methods that belong to the group mentioned in the <include> element. Only the groups included will be searched for in the test classes added in <classes>. You can also exclude a group using <exclude> element, like we have excluded feature2 tests.

feature1Testng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite">
	<test name="feauture1">
		<groups>
			<run>
				<include name="feature1" />
				<exclude name="feature2" />
			</run>
		</groups>
		<classes>
			<class name="com.javarticles.testng.UITesting" />
			<class name="com.javarticles.testng.UnitLevelTesting" />
			<class name="com.javarticles.testng.FunctionalTesting" />
		</classes>
	</test>	
</suite>

Output:

[TestNG] Running:
  C:\javarticles_ws\testNGGroup\feature1Testng.xml

UI1 testing: feature1
Unit test2: feature1
Scenario1: feature1
Scenario2: feature1

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

Below configuration file represents feature3 tests.

feature3Testng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite">	
	<test name="feauture3">
		<groups>
			<run>
				<include name="feature3" />
			</run>
		</groups>
		<classes>
			<class name="com.javarticles.testng.UITesting" />
			<class name="com.javarticles.testng.UnitLevelTesting" />
			<class name="com.javarticles.testng.FunctionalTesting" />
		</classes>
	</test>
</suite>

Output:

[TestNG] Running:
  C:\javarticles_ws\testNGGroup\feature3Testng.xml

UI3 testing: feature3

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

Below configuration file represents feature1 and feature2 tests.

feature1And2Testng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite">	
	<test name="feauture1And2">
		<groups>
			<run>
				<include name="feature1" />
				<include name="feature2" />
			</run>
		</groups>
		<classes>
			<class name="com.javarticles.testng.UITesting" />
			<class name="com.javarticles.testng.UnitLevelTesting" />
			<class name="com.javarticles.testng.FunctionalTesting" />
		</classes>
	</test>
</suite>

Output:

[TestNG] Running:
C:\javarticles_ws\testNGGroup\feature1And2Testng.xml

UI1 testing: feature1
UI2 testing: feature1 and feature2
Unit test1: feature1 and feature2
Unit test2: feature1
Unit test2: feature2
Scenario1: feature1
Scenario2: feature1
Scenario3: feature1 and feature2

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

How to TestNG group in Eclipse?

Create a new run configuration under TestNg.

  1. Open Eclipse and go to Run->Run Coniguraions.
  2. Select TestNG and click on the new coniguraion to enter a coniguraion name.
  3. Go to the Project secion, click on the Browse buton and select the project.
  4. Go to the Groups secion and click on the Browse buton to show all the groups used.
  5. Select the group(s) which you would like to run.
  6. Click on the Apply buton and then click on Run.
TestNG group configuration in eclipse

TestNG group configuration in eclipse

Download Source Code

You can download the source code here: testNGGroup.zip

Share.

Leave A Reply