Tests Depends on Groups in TestNG
Tests Depends on Groups in TestNG will discuss about how a test can dependent on groups or multiple groups. This feature help in executing a set of groups to be executed before a test method. Group dependency only works if the depend-on-group is part of the same class or any other classes which are in the same package.
Test depends on Single Group:
A test can dependent on a single group or more than a single group. In this section we will see how a test depends on single group.
Below is the sample program:
import org.testng.annotations.Test; public class DependsOnSingleGroup { @Test(dependsOnGroups={"singleGroup"}) public void testOne() { System.out.println("Depends on SingleGroup Methods"); } @Test(groups={"singleGroup"}) public void testTwo() { System.out.println("In testTwo which is in SingleGroup"); } @Test(groups={"singleGroup"}) public void testThree() { System.out.println("In testThree which is in SingleGroup"); } }
In the above program, we have three test methods called “testOne”,”testTwo”, and “testThree” and testOne depends on a group called “singleGroup”. But testTwo and testThree are belong to singleGroup. This can be achieved by one of the @Test attributes called “dependsOnGroups” as shown in the program. When you set this attribute, then it will look for the dependent groups to execute first before the dependency test. If you execute the above program testTwo and testThree will execute first then testOne as the both belongs to a group called “singleGroup” and testOne depends on this group.
Test depends on Multiple Groups:
As discussed in the previous section a method can depends on single group or multiple groups. In this section we will see how a test can depends on multiple groups.
Below is the sample program:
import org.testng.annotations.Test; public class DependsOnMultipleGroups { @Test(dependsOnGroups={"firstGroup","secondGroup"}) public void testOne() { System.out.println("Depends on Multiple Groups called firstGroup and secongGroup"); } @Test(groups={"firstGroup"}) public void testTwo() { System.out.println("In testTwo which is in firstGroup"); } @Test(groups={"secondGroup"}) public void testThree() { System.out.println("In testThree which is in secondGroup"); } }
In the above program we have three test methods called “testOne”,”testTwo” and “testThree”. testOne depends on two groups called “firstGroup”,”secondGroup”. And testTwo belongs to firstGroup and testThree belongs to secondGroup. This can be achieved by one of the @Test attributes called “dependsOnGroups” as shown in the program. When you set this attribute, then it will look for the dependent groups to execute first before the dependency test. If you execute the above program testTwo and testThree will execute first then testOne as the both belongs to different groups called “firstGroup”, “secondGroup” and testOne depends on these groups.
Test that depends on other Classes from the same package:
If you want a test method depends on other class methods and you want to execute that method then the both classes should be in the same package.
Below is the sample program:
FirstClass, a test Method from this class is depends on another class which is in same package:
package com.test.package; import org.testng.annotations.Test; public class FirstClass { @Test(dependsOnGroups={"anotherClassGroup","sameClassGroup"}) public void testOne() { System.out.println("Depends on the same class methods and other class methods which are in same pacakge."); } @Test(groups={"sameClassGroup"}) public void testTwo() { System.out.println("Base test from the same class"); } }
SecondClass:
package com.test.package; import org.testng.annotations.Test; public class SecondClass { @Test(groups={"anotherClassGroup"}) public void testThree() { System.out.println("Base test from the Another class"); } }
In the above, we have two classes from the same package called FirstClass and SecondClass. And testOne method from FirstClass is depends on two groups and one group from FirstClass and another group from Second Class. Once you execute the testOne then it will execute the two groups first then testOne will exeucte.
This we can control the execution flow while running the test suite.
Please watch the youtube video for better understanding.