how to Set Test Dependency in TestNG

In this Section, we will Understand How to Set Testcase Dependency in TestNG, it allows a test method to depend on a single or a group of test methods.

What Is TestCase Dependency in TestNG?

Dependency is a TestNG feature that allows a test method to rely on one or more another test case to Execute.

Set Testcase Dependency in TestNG

To Set Dependency on a Testcase, we are going to Use attribute name (dependsOnMethods="Function_Name")

When We execute a Testcase without Dependency on other Testcase completion, all the Testcases Defined in the Program wouldn’t follow the ordered Execution and in case we want a certain function to be executed before the other function execution we can use (dependsOnMethods="Function_Name") to create Dependency.

  • Create a New Class Name “Dependency”.
  • Create Four Methods with Print Statements.

Let’s Take an Example, the Following Java Program which Doesn’t have any Dependency Set on Testcases.

Dependency.java
import org.testng.annotations.Test;

public class Dependency {
    @Test 
     public void TestOne() {
          System.out.println("This is TestOne ");//Print Statement
      }
     @Test
     public void TestTwo() {
          System.out.println("This is TestTwo ");//Print Statement
      }
     @Test
     public void TestThree() {
          System.out.println("This is TestThree ");//Print Statement
      }
     @Test
     public void TestFour() {
          System.out.println("This is TestFour ");//Print Statement
      }
}

In the Above Example, We have Defined Four Testcases with Each Having There Own Print Statements to Show the Execution Flow Without Dependency set on Each Testcases.

OutPut

As You can See in Output The Method Didn’t Follow any Order and the Testcase TestFour()is executed First followed by TestTwo(),TestOne(),TestThree()Repectively.

which is Not the Order we Required So the (dependsOnMethods="Function_name")is Used to Create Dependency to Follow a set Sequence.

  • Create a New Class Name “Dependency”.
  • and Create Four Methods with Print Statements.
  • Now Create Dependency in such a way that it Follow the execution flow like this TestOne()–>TestTwo()–>TestThree()–>TestFour()
Dependency.java
import org.testng.annotations.Test;

public class Dependency {
    @Test 
     public void TestOne() {
          System.out.println("This is TestOne ");
      }
     @Test (dependsOnMethods="TestOne") // A Dependency is created here of TestOne() Method until the TestOne() Method is Executed this would be executed
     public void TestTwo() {
          System.out.println("This is TestTwo ");
      }
     @Test (dependsOnMethods="TestTwo") // A Dependency is created here of TestTwo() Method until the TestTwo() Method is Executed this would be executed
     public void TestThree() {
          System.out.println("This is TestThree ");
      }
     @Test (dependsOnMethods="TestThree") // A Dependency is created here of TestThree() Method until the TestThree() Method is Executed this would be executed
     public void TestFour() {
          System.out.println("This is TestFour ");
      }
}

if a dependent method fails, all the subsequent test methods will be skipped, NOT failed.

Output

We Successfully Demonstrated Setting Dependency on Testcases in TestNG.