TestNG Groups

Filed Under: TestNG

TestNG Groups is one of its very important and useful features. We can specify groups for TestNG methods, it can be used with @Before, @After and @Test methods.

TestNG Groups

Once we have defined the groups for TestNG methods, we can run specific groups. We can include groups to be executed, exclude groups. We can also create a group of groups. Let’s create a TestNG test class with few methods and assign them to different groups.


package com.journaldev.groups;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestNGGroupsExample {

	@Test(groups = "foo")
	public void foo() {
		System.out.println("foo");
	}

	@Test(groups = "bar")
	public void bar() {
		System.out.println("bar");
	}

	@Test(groups = { "alpha", "sanity" })
	public void alpha() {
		System.out.println("alpha");
	}

	@Test(groups = { "beta", "integration" })
	public void beta() {
		System.out.println("beta");
	}

	@BeforeClass(groups = "integration")
	public void beforeIntegrationTests() {
		System.out.println("Before Running integration test methods");
	}

	@AfterClass(groups = "integration")
	public void afterIntegrationTests() {
		System.out.println("Before Running integration test methods");
	}

}

Running TestNG Groups using Eclipse TestNG Run Configuration

When we execute a TestNG class in Eclipse, it executes all the groups. We can create a Run Configuration for specific groups to execute. Go to Run | Run Configurations.

Browse for your project, then select the Groups checkbox and click on Browse button. It will show you all the groups defined in the project.

eclipse testng available groups

Select the groups to execute and you should see configuration like below image.

Eclipse TestNG Plugin Run Groups

Click on “Apply” button to save the configuration. Click on “Run” button to run the configuration, which will execute only the specified groups.

You should get following output in the Eclipse console.


[RemoteTestNG] detected TestNG version 6.14.3
bar
foo
PASSED: bar
PASSED: foo

===============================================
    GRP-bar,foo
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
TestNG-Examples by groups
Total tests run: 2, Failures: 0, Skips: 0
===============================================

TestNG XML Suite Groups Example

We can configure our test suite to run specific groups using groups element. Below TestNG XML suite will run only foo and bar group methods.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestNGXMLTest Test Suite">
	<groups>
		<run>
			<include name="foo"></include>
			<include name="bar"></include>
		</run>
	</groups>
	<test name="TestNGXMLTest Test">
		<classes>
			<class name="com.journaldev.groups.TestNGGroupsExample" />
		</classes>
	</test>
</suite>

TestNG XML Include Exclude Groups Example

Sometimes our methods will be part of multiple groups. If the method is part of both included and excluded groups, then it will be excluded from execution.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestNGXMLTest Test Suite">
	<groups>
		<run>
			<include name="alpha"></include>
			<include name="beta"></include>
			<exclude name="integration"></exclude>
		</run>
	</groups>
	<test name="TestNGXMLTest Test">
		<classes>
			<class name="com.journaldev.groups.TestNGGroupsExample" />
		</classes>
	</test>
</suite>

Above test suite will not execute beta() method because one of its groups is in excluded groups list.

TestNG Groups Regular Expression

TestNG groups configuration supports regular expression. If we change groups include to include name=".*o.*", then it will only execute foo() and beta() methods. It’s because they belong to the groups whose name conatins letter “o”.

TestNG Default Group

If we want all our Test class methods to be part of a specific group, we can configure it at the class level.


@Test(groups="default")
public class TestNGGroupsExample {
// test methods
}

TestNG Group of Groups

We can define group of groups in the TestNG XML file. Then we can use it in the test suite. Note that this can be done inside only at test level, not at suite level.

Let’s look at a quick example of defining groups in the TestNG XML file and then using them.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestNGXMLTest Test Suite">

	<test name="TestNGXMLTest Test">
		<groups>
			<define name="include-groups">
				<include name="sanity" />
				<include name="integration" />
			</define>
			<define name="exclude-groups">
				<include name="foo" />
				<include name="bar" />
			</define>
			<run>
				<include name="include-groups" />
				<exclude name="exclude-groups" />
			</run>
		</groups>
		<classes>
			<class name="com.journaldev.groups.TestNGGroupsExample" />
		</classes>
	</test>
</suite>

That’s all for TestNG groups example. They are very helpful in categorizing our tests and run them for different levels of verification.

You can download the example code from our GitHub Repository.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages