TestNG dependsOnGroups Example

In this post we will see TestNG groups dependency example showing a test depending on groups of tests methods using @Test annotation’s dependsOnGroups and alwaysRun attributes. Let’s get going.


dependsOnGroups : dependsOnGroups attribute on a test method [test1 e.g.] specifies all the groups [group1, group2,..] this test method depends on. It means test1 will start execution only after all the tests in all the groups it depends on executed successfully. If any of the tests in groups specified via dependsOnGroups attribute failed, then test1 will be skipped.

alwaysRun=true : Setting alwaysRun attribute on a test method to true forces the execution of this test even if the some tests in the group it depends on were failed.


Let’s take a trivial test class to demonstrate this feature.

package com.websystique.testng;

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

public class TesNGDependsOnGroupsExample {
	
	@BeforeGroups("security")
	public void setUpSecurity() {
		System.out.println("setUpSecurity()");
	}
 
	@AfterGroups("security")
	public void tearDownSecurity() {
		System.out.println("tearDownSecurity()\n");
	}

	@BeforeGroups("database")
	public void setUpDatabase() {
		System.out.println("setUpDatabase()");
	}
 
	@AfterGroups("database")
	public void tearDownDatabase() {
		System.out.println("tearDownDatabase()\n");
	}
 
	@BeforeGroups(value="ui")
	public void setUpUI() {
		System.out.println("setUpUI()");
	}
 
	@AfterGroups(value="ui")
	public void tearDownUI() {
		System.out.println("tearDownUI()\n");
	}

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

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

	@Test(groups= "security")
	public void accessHomePage() {
		System.out.println("accessHomePage()");
	}
 
	@Test(groups= "security")
	public void accessAdminPage() {
		System.out.println("accessAdminPage()");
		throw new RuntimeException();
	}

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

	@Test(groups= "ui")
	public void openFileDialog() {
		System.out.println("openFileDialog()");
	}
	
	@Test(dependsOnGroups= {"ui"})
	public void uiGroupDependentTest(){
		System.out.println("uiGroupDependentTest()");
	}

	
	@Test(dependsOnGroups= {"security","database"})
	public void backendGroupDependentTest1(){
		System.out.println("backendGroupDependentTest1()");
	}

	@Test(dependsOnGroups= {"security","database"}, alwaysRun=true)
	public void backendGroupDependentTest2(){
		System.out.println("backendGroupDependentTest2()");
	}

}

Above test class contains three groups security,database & ui. Then we have bunch of tests that belong to different groups. And at last, we have three methods which depends on different groups.
Notice the accessAdminPage test (from security group) which will throw a RuntimeException. That means group ‘security’ will not be successfully executed.

Run above tests class using mvn clean test or TestNG eclipse plugin.

Following is the output of above test run:

setUpSecurity()
accessAdminPage()
accessHomePage()
tearDownSecurity()

setUpUI()
openConfirmationDialog()
openFileDialog()
tearDownUI()

setUpDatabase()
testDelete()
testInsert()
testUpdate()
tearDownDatabase()

backendGroupDependentTest2()

uiGroupDependentTest()

PASSED: accessHomePage
PASSED: openConfirmationDialog
PASSED: openFileDialog
PASSED: testDelete
PASSED: testInsert
PASSED: testUpdate
PASSED: backendGroupDependentTest2
PASSED: uiGroupDependentTest
FAILED: accessAdminPage
java.lang.RuntimeException
	at com.websystique.testng.TesNGDependsOnGroupsExample.accessAdminPage(TesNGDependsOnGroupsExample.java:62)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
	at org.testng.TestRunner.privateRun(TestRunner.java:767)
	at org.testng.TestRunner.run(TestRunner.java:617)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
	at org.testng.SuiteRunner.run(SuiteRunner.java:240)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
	at org.testng.TestNG.run(TestNG.java:1057)
	at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

SKIPPED: backendGroupDependentTest1

===============================================
    Default test
    Tests run: 10, Failures: 1, Skips: 1
===============================================


===============================================
Default suite
Total tests run: 10, Failures: 1, Skips: 1
===============================================

It’s evident from above output that all @Test methods that belongs to a group were executed. Now among the methods that depends on different groups, uiGroupDependentTest executed successfully as all methods of ‘ui’ group executed successfully. backendGroupDependentTest1 is skipped as one of the groups it depends on [‘security’] have not been executed successfully(accessHomePage failed due to exception). backendGroupDependentTest2 , on the other hand, was completed successfully due to force execution by using alwaysRun=true.

That’s it.

References