Dependency Test in TestNG

Dependency Test in TestNG

Sometimes, you may need to invoke methods in a Test case in a particular order or you want to share some data and state between methods. This kind of dependency is supported by TestNG as it supports the declaration of explicit dependencies between test methods.

In simple, Dependency means if one @Test method fails or skipped from execution then its dependent @Test method must not be executed.

TestNG allows you to specify dependencies either with:

  • Using attributes dependsOnMethodsin @Test annotations OR
  • Using attributes dependsOnGroupsin @Test annotations.

Take a look over the below example:

package TestNgAdv;
import org.testng.Assert;
import org.testng.annotations.Test;
public class SetPriority2 {@Test(priority = 1)
	public void Login() {
		System.out.println("LogIn Test code.");
		Assert.assertTrue(6 > 7, "condition is false");
	}@Test(priority = 2, dependsOnMethods = {
	public void checkMail() {
		System.out.println("checkMail Test code.");
	}@Test(priority = 3, dependsOnMethods = {
	public void LogOut() {
		System.out.println("LogOut Test code.");
	}@Test(priority = 4)
	public void Validation() {
		System.out.println("Login Validation Test code.");

In the above program, LogOut() method is dependent on login() and checkMail() methods. If these two methods get failed or skipped then LogOut() method will not execute.


<!DOCTYPE suite SYSTEM "" >
<suite name="Regression Suite" > 
<test name=" Regression Test" > 
<class name="TestNgAdv.SetPriority2" /> 

Leave a Reply

Your email address will not be published. Required fields are marked *