TestNG Tutorials 61: Dependency in TestNG – Creating Dependency Among Test Methods – DependsOnGroup
In previous post, We have learnt to Establish dependency among test methods. In this post, we will see another concept in Dependency called DependsOnGroup.
Consider a scenario:
You have created automation scripts for Integration testing. We know that in integration testing, data flows from one module to another. You divide your test methods as per group. Say Group 1 and group 2. You run Group 1 methods first followed by Methods of Group 2. We can achieve the same in TestNG using DependsOnGroup.
So when we mention a group name or list of group names for a @Test annotated method, all methods of mentioned groups will run first. We will see an example below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package Dependecy; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class DependsOnGroupsExample { // Test method belong to preSetupTestA @Test(groups= {"preSetupTestA"}) public void methodA() { System.out.println("MethodA"); } //Test method belong to preSetupTestA @Test(groups= {"preSetupTestA"}) public void methodB() { System.out.println("MethodB"); } // Test method belong to preSetupTestB @Test(groups= {"preSetupTestB"}) public void methodC() { System.out.println("MethodC"); } // Test method belong to preSetupTestB @Test(groups= {"preSetupTestB"}) public void methodD() { System.out.println("MethodD"); } // Test method which is dependent of other groups @Test(dependsOnGroups = {"preSetupTestB", "preSetupTestA"}) public void finalTest() { System.out.println("Final Test."); } } |
Output:
[RemoteTestNG] detected TestNG version 6.14.2 MethodA MethodB MethodC MethodD Register on Facebook. PASSED: methodA PASSED: methodB PASSED: methodC PASSED: methodD PASSED: registerOnFacebook =============================================== Default test Tests run: 5, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 5, Failures: 0, Skips: 0 ===============================================
You need to note here that when we mention group names as dependent, TestNG will run them in some indefinite order. So if you want to maintain order for methods of other groups, you need to use dependsOnMethods or priority.
Example is as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package Dependecy; import org.testng.annotations.Test; public class DependsOnGroupsExample { // Test method belong to preSetupTestA @Test(groups= {"preSetupTestA"}, priority = 2) public void methodA() { System.out.println("MethodA"); } //Test method belong to preSetupTestA @Test(groups= {"preSetupTestA"}, priority= 1) public void methodB() { System.out.println("MethodB"); } // Test method belong to preSetupTestB @Test(groups= {"preSetupTestB"}, priority = 4) public void methodC() { System.out.println("MethodC"); } // Test method belong to preSetupTestB @Test(groups= {"preSetupTestB"}, priority = 3) public void methodD() { System.out.println("MethodD"); } // Test method which is dependent of other groups @Test(dependsOnGroups = {"preSetupTestB", "preSetupTestA"}) public void finalTest() { System.out.println("Final Test."); } } |
[RemoteTestNG] detected TestNG version 6.14.2 MethodB MethodA MethodD MethodC Final Test. PASSED: methodB PASSED: methodA PASSED: methodD PASSED: methodC PASSED: finalTest =============================================== Default test Tests run: 5, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 5, Failures: 0, Skips: 0 ===============================================
So you can use dependsOnGroup attribute, when you need all test methods of a specific group or multiple groups to be executed before.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium
Author: Amod Mahajan
A software Tester who is paid to judge products developed by others. Writing technical posts and creating YouTube videos are my hobbies.