Dependency Testing in TestNG: How to Run a Test only if Dependent Test Method Has Passed

Print View Mobile View

TestNG offers a feature that allows us to make a method depend on another for execution. So if a dependent method fails, all the subsequent methods would be skipped. Dependency can be specified using annotations or in testng.xml file.

Dependency with annotations can be made on the result of a particular method (dependOnMethods) or on a group of methods(dependsOnGroups). These methods would only work if the dependent method is part of the same class or in one of the inherited class.

Let’s have a look at each of the methods now.

dependOnMethods in TestNG

dependOnMethods makes a method depend on the result of one or more methods. Here’s a simple example to understand its working:

File: DependencyDemo.java

package TestNGDemo;

import org.testng.annotations.Test;

public class DependencyDemo {
    @Test(dependsOnMethods = { "TestB", "TestC" })
    public void TestA() {
        System.out.println("Test A");
    }
 
    @Test(dependsOnMethods = { "TestC" })
    public void TestB() {
        System.out.println("Test B");
    }
    
    @Test
    public void TestC() {
        System.out.println("Test C");
    }
}

In the above code, TestA() depends on both TestB() and TestC(), and TestB() depends on method TestC(). This shows both single and multiple test method dependency. Output of the test is:
TestNG Dependency

Dependent methods with Inheritance

Here’s an example where the dependent methods are part of an inherited class.

File: DependencyDemo.java

package TestNGDemo;

import org.testng.annotations.Test;

public class DependencyDemo {
	@Test(dependsOnMethods = { "TestB" })
	public void TestA() {
		System.out.println("Test A");
	}

	@Test
	public void TestB() {
		System.out.println("Test B");
	}
}

class DependentChildClass extends DependencyDemo
{
	@Test(dependsOnMethods = { "TestA" })
	public void TestC() {
		System.out.println("Test C in Child");
	}
}

Here, TestC() depends on TestA() from the parent class for execution. TestA() again depends on TestB(). So, the output of this example would also be similar to the first example we saw above.

These tests can also be executed together in a suite by creating an XML file as shown below:

File: tesng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestDependency">

	<test name="TestCase1">

		<classes>
			<class name="TestNGDemo.DependentTestExamples">
			</class>
			<class name="TestNGDemo.DependencyDemo">
			</class>
		</classes>

	</test>

</suite>

dependsOnGroups in TestNG

dependsOnGroups make a particular method or a group of methods depend on the result of another group of methods. In the example below, we are using dependsOnGroups as well as dependsOnMethods.

File: DependencyDemo

package TestNGDemo;

import org.testng.annotations.Test;

public class DependencyDemo {
    @Test(dependsOnGroups = { "GroupB", "GroupC" })
    public void TestA() {
        System.out.println("Test A");
    }
 
    @Test(dependsOnMethods="TestC", groups = { "GroupB" })
    public void TestB() {
        System.out.println("Test B");
    }
 
    @Test(groups = { "GroupC" })
    public void TestC() {
        System.out.println("Test C");
    }
}

TestA() depends on “GroupA” and “GroupB,” whereas TestB() is part of “GroupB” and it also depends on TestC() method.