TestNG Test Dependencies (2024) | TechGeekNext

TestNG Test Dependencies (2024)

In this tutorial, we will understand TestNG Test Dependencies and Different types of Test Dependencies in TestNG.

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:

  1. Single Dependent Test Methods In TestNG
  2. Multiple Dependent Test Methods In TestNG
  3. Inherited Dependent Test Methods In TestNG
  4. Group Dependent Tests In TestNG

  1. 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.
    package 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");
    	}
    
    }
    
    Output

    In the below test result, you can see the testConnectDB printed before testAutheticateUser. This shows that the testAutheticateUser method got processed after testAutheticateUser as it depends on testAutheticateUser. Single Dependent Test Methods In TestNG

  2. 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.
    package 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.");
    	}
    }
    
    Output

    In the below-given result, we can see that testGetResource method depends on testConnectDB and testAutheticateUser methods to get the resource. Multiple Dependent Test Methods In TestNG

  3. 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() {
    	}
    
    }
    
    package com.techgeeknext.testcases;
    
    import org.testng.annotations.Test;
    
    public class DependentClass extends BaseClass {
    	@Test(dependsOnMethods = { "testFuncOne" })
    	public void testFuncThree() {
    	}
    
    	@Test
    	public void testFuncFour() {
    	}
    }
    
    Output

    Run the DependentClass test cases and can see all test cases with depends methods from base class and from inherited class got executed successfully. Inherited Dependent Test Methods In TestNG

  4. 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.
    package 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.");
    	}
    }
    
    Output

    In the below test result, you can see the emp-groupTest methods got processed first and then it's dependsOnGroups method. Group Dependent Tests In TestNG

  5. 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.

    package 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.");
    	}
    
    }
    
    Output

    In this scenario all the test methods will execute without any dependency test method with alphabetic methods name. TestNG Dependent Tests in XML Suite

    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>
    
    TestNG Dependent Tests in XML Suite

Recommendation for Top Popular Post :