Main Tutorials

TestNG – Groups Test

In this tutorial, we will show you how to do the group testing in TestNG.

1. Groups on Methods

Review a test group example.

  1. runSelenium() and runSelenium1() are belong to group selenium-test.
  2. testConnectOracle() and testConnectMsSQL() are belong to group database.
  3. runFinal() will be executed if groups selenium-test and database are passed.
TestGroup.java

package com.mkyong.testng.examples.group;

import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

public class TestGroup {

	@BeforeGroups("database")
	public void setupDB() {
		System.out.println("setupDB()");
	}
	
	@AfterGroups("database")
	public void cleanDB() {
		System.out.println("cleanDB()");
	}
	
	@Test(groups= "selenium-test")
	public void runSelenium() {
		System.out.println("runSelenium()");
	}

	@Test(groups= "selenium-test")
	public void runSelenium1() {
		System.out.println("runSelenium()1");
	}
	
	@Test(groups = "database")
	public void testConnectOracle() {
		System.out.println("testConnectOracle()");
	}

	@Test(groups = "database")
	public void testConnectMsSQL() {
		System.out.println("testConnectMsSQL");
	}
	
	@Test(dependsOnGroups = {"database","selenium-test"})
	public void runFinal() {
		System.out.println("runFinal");
	}
	
}

Output



//group = selenium-test
runSelenium()
runSelenium()1

//group = database
setupDB()
testConnectMsSQL
testConnectOracle()
cleanDB()

//dependsOnGroups = database, selenium-test
runFinal

PASSED: runSelenium
PASSED: runSelenium1
PASSED: testConnectMsSQL
PASSED: testConnectOracle
PASSED: runFinal

2. Groups on Class

The “Group” can be applied on class level. In below example, every public method of this class “TestSelenium” is belong to group selenium-test.

TestSelenium.java

package com.mkyong.testng.examples.group;

import org.testng.annotations.Test;

@Test(groups= "selenium-test")
public class TestSelenium {

	public void runSelenium() {
		System.out.println("runSelenium()");
	}

	public void runSelenium1() {
		System.out.println("runSelenium()1");
	}
	
}

Create an XML file to run 2 test classes.

testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="TestAll">

	<test name="final">
		<classes>
			<class name="com.mkyong.testng.examples.group.TestSelenium" />
			<class name="com.mkyong.testng.examples.group.TestGroup" />
		</classes>
	</test>
	
	<!-- Run test method on group "selenium" only -->
	<test name="selenium">
	
		<groups>
			<run>
				<include name="selenium-test" />
			</run>
		</groups>
  
		<classes>
			<class name="com.mkyong.testng.examples.group.TestSelenium" />
			<class name="com.mkyong.testng.examples.group.TestGroup" />
		</classes>
		
	</test>
	
</suite>

Output



//test name = final
runSelenium()
runSelenium()1
setupDB()
testConnectMsSQL
testConnectOracle()
cleanDB()
runFinal

//test name = selenium
runSelenium()
runSelenium()1

===============================================
TestAll
Total tests run: 7, Failures: 0, Skips: 0
===============================================

3. Misc Examples

3.1 A test method can belong to multiple groups.


	@Test(groups = {"mysql","database"})
	public void testConnectMsSQL() {
		System.out.println("testConnectMsSQL");
	}

3.2 The above result is executed via Eclipse TestNG Plugin.

eclipse-testng

References

  1. TestNG : @Test
  2. TestNG Eclipse plugin

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
GokuGT
9 years ago

Hi,

I think the output of second example goes wrong, I wirte and did the example on IDE and the output goes:

runSelenium()
runSelenium()1
runSelenium()
runSelenium()1
setupDB()
testConnectMsSQL
testConnectOracle()
cleanDB()
runFinal
runSelenium()
runSelenium()1
runSelenium()
runSelenium()1

===============================================

TestAll

Total tests run: 11, Failures: 0, Skips: 0

===============================================

ngowda
9 years ago

Hi,
how can we create a Test suite in TestNG and eclipse

Madiraju Krishna Chaitanya
10 years ago

Hi Mkyong Sir,Thanks a LOT for this tutorials on TestNG.It was very useful.Please take some tutorials on Frameworks too(Keyword Driven,Data Driven and Hybrid Driven) for Selenium Projects.Thanks in Advance.