TestNG Test Dependencies (2024)
In this tutorial, we will understand TestNG Test Dependencies and Different types of Test Dependencies in TestNG.
TestNG Tutorial :
Q: What are TestNG Test dependencies?
Ans:
We are often required to run tests in a particular order and even one test should run only when another test is run in TestNG is called Test Dependencies. It is a test method to depend on a single or group of the test methods in the TestNG.
Test Dependency will help in implementing a group of tests to be implemented before a test method. Tests dependency is only applicable if depend on method is a part of any inherited base class or part of the same class.
Different types of Test Dependencies in TestNG
Following are the different types of Test Dependencies in TestNG are:
- Single Dependent Test Methods In TestNG
- Multiple Dependent Test Methods In TestNG
- Inherited Dependent Test Methods In TestNG
- Group Dependent Tests In TestNG
Single Dependent Test Methods In TestNG
When a single test depends on another test is called a Single Dependent Test in TestNG. This method proceeds with the use of depend on method.
Outputpackage com.techgeeknext.testcases; import org.testng.annotations.Test; public class LoginTestCases { @Test(dependsOnMethods = { "testConnectDB" }) public void testAuthenticateUser() { System.out.println("#a Test Authenticate User"); } @Test public void testConnectDB() { System.out.println("#b Test Connection"); } }
In the below test result, you can see the
testConnectDB
printed beforetestAutheticateUser
. This shows that thetestAutheticateUser
method got processed aftertestAutheticateUser
as it depends ontestAutheticateUser
.Multiple Dependent Test Methods In TestNG
As a part of the dependency support, Multiple Dependent Test Method is a very well-supported quality by TestNG. Sometimes for the test method to depend upon multiple other methods is required.
Outputpackage com.techgeeknext.testcases; import org.testng.annotations.Test; public class LoginTestCases { @Test(dependsOnMethods = { "testConnectDB", "testAuthenticateUser" }) public void testGetResource() { System.out.println("a# Test Get Resource."); } @Test public void testConnectDB() { System.out.println("b# Test Connection."); } @Test public void testAuthenticateUser() { System.out.println("c# Test Authenticate User."); } }
In the below-given result, we can see that
testGetResource
method depends ontestConnectDB
andtestAutheticateUser
methods to get the resource.Inherited Dependent Test Methods In TestNG
In the above examples, we have seen in which the dependent test methods were part of the same class. Dependency on the test methods can be performed when test methods belong to the same or any inherited base classes.Now Let's see the example when dependent tests are part of the inherited base class performing the test methods in the TestNG.
package com.techgeeknext.testcases; import org.testng.annotations.Test; public class BaseClass { @Test(dependsOnMethods = { "testFuncTwo" }) public void testFuncOne() { } @Test public void testFuncTwo() { } }
Outputpackage com.techgeeknext.testcases; import org.testng.annotations.Test; public class DependentClass extends BaseClass { @Test(dependsOnMethods = { "testFuncOne" }) public void testFuncThree() { } @Test public void testFuncFour() { } }
Run the
DependentClass
test cases and can see all test cases with depends methods from base class and from inherited class got executed successfully.Group Dependent Tests In TestNG
In TestNG dependent tests are also performed depending on the Group of tests. This method is applicable when a group of test methods is implemented before the dependent test method.
Outputpackage com.techgeeknext.testcases; import org.testng.annotations.Test; public class LoginTestCases { @Test(dependsOnGroups = { "emp-group" }) public void testGetResource() { System.out.println("a# Test Get Resource."); } @Test(priority = 1, groups = { "emp-group" }) public void testConnectDB() { System.out.println("b# Test Connection."); } @Test(priority = 2, groups = { "emp-group" }) public void testAuthenticateUser() { System.out.println("c# Test Authenticate User."); } }
In the below test result, you can see the
emp-groupTest
methods got processed first and then it'sdependsOnGroups
method.TestNG Dependent Tests in XML Suite
Dependencies created between groups in the XML file in TestNG are known as TestNG Dependent Tests in XML Suite. Here dependency commands over to the XML file.When there are multiple groups in the TestNG file, the dependent tests in between them in the XML file can be created.
TestNG test case file for multiple groups.
Outputpackage com.techgeeknext.testcases; import org.testng.annotations.Test; public class LoginTestCases { @Test(groups = { "resource" }) public void testGetResource() { System.out.println("a# Test Get Resource."); } @Test(groups = { "db" }) public void testConnectDB() { System.out.println("b# Test Connection."); } @Test(groups = { "user" }) public void testAuthenticateUser() { System.out.println("c# Test Authenticate User."); } }
In this scenario all the test methods will execute without any dependency test method with alphabetic methods name.
Now create XML for above test case with dependency on each method.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name="TestNG Suite"> <test name="Employee Dependent Test"> <groups> <dependencies> <!-- get resource depends on user should be authenticated. --> <group depends-on="user" name="resource"></group> <!-- user authentication needs database connection --> <group depends-on="db" name="user"></group> </dependencies> </groups> <classes> <class name="com.techgeeknext.testcases.LoginTestCases" /> </classes> </test> </suite>