How to create dependency between Selenium Test Cases using TestNG

 
 
In the previous tutorial, we have discussed about How to disable Test Cases usingTestNG. In this tutorial, we will see how we can create dependency between Selenium Test Cases using TestNG.
Imagine there is a situation that a test case can only be executed if a particular test case executes successfully. This can be achieved in TestNG by dependsOnMethod().
 
Below is an example.
To Run the TestNG program, right click on the Java Program – here it is TestNGMethodDependencyDemo select Run As TestNG Test
 
 

package TestNGDemo; 
import org.testng.annotations.Test;
public class TestNGMethodDependencyDemo {

      @Test
      public static void FirstTest() {
            System.out.println("This is Test Case 1");
      } 

      @Test(dependsOnMethods = "FirstTest") 
      public static void SecondTest() {
          System.out.println("This is Test Case 2 and will be executed after Test Case 1 sucessfully executed");
     } 
          
      @Test 
      public static void ThirdTest() {
           System.out.println("This is Test Case 3");
      } 

     @Test 
      public static void FourthTest() {
            System.out.println("This is Test Case 4"); 
     }
}

Output 
This is Test Case 1
This is Test Case 4
This is Test Case 3
This is Test Case 2 and will be executed after Test Case 1 sucessfully executed
PASSED: FirstTest
PASSED: FourthTest
PASSED: ThirdTest
PASSED: SecondTest
============================================
    Default test
   Tests run: 4, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0

In the above scenario, Test Case 2 is dependent on Test CASE 1. If Test Case 1 fails, then Test Case 2 will skip.

package TestNGDemo;

import org.testng.annotations.Test;
public class TestNGMethodDependencyErrorDemo {

      @Test
      public static void FirstTest() {
            System.out.println("This is Test Case 1");
            throw new RuntimeException();
      }

      @Test(dependsOnMethods = "FirstTest")
      public static void SecondTest() {
          System.out.println("This is Test Case 2 and will be executed after Test Case 1 sucessfully executed");
     }

     @Test
     public static void ThirdTest() {
          System.out.println("This is Test Case 3");
     }

      @Test
      public static void FourthTest() {
            System.out.println("This is Test Case 4");
    }
}

Output 
This is Test Case 1

This is Test Case 4
This is Test Case 3
PASSED: FourthTest
PASSED: ThirdTest
FAILED: FirstTest
java.lang.RuntimeException
SKIPPED: SecondTest
java.lang.Throwable: Method TestNGMethodDependencyErrorDemo.SecondTest()[pri:0, instance:TestNGDemo.TestNGMethodDependencyErrorDemo@574b560f] depends on not successfully finished methods

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s