Dependent Test in TestNG

How to define test dependency in TestNG

74

Often we see dependency in test cases. one test case is dependent on output or end state of the other test case. TestNG provides an option to perform it.

two different conditions TestNG support:

  1. Execute test case after successful execution of the dependent test case
  2. Skip test case after the failure of the dependent test case.
Execute test case after successful execution of the dependent test case

 

package TestNG;

import org.testng.annotations.Test;

public class dependentTestSuccess {

 @Test
 public void method1() {
 System.out.println("This is method 1");
 }

@Test(dependsOnMethods = { "method1" })
 public void method2() {
 System.out.println("This is method 2");
 }


}

 

output:

[RemoteTestNG] detected TestNG version 6.14.3
This is method 1
This is method 2
PASSED: method1
PASSED: method2

===============================================
 Default test
 Tests run: 2, Failures: 0, Skips: 0
===============================================

===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

 

Skip test case after the failure of the dependent test case.

 

package TestNG;

import org.testng.annotations.Test;

public class dependentTestFailure {
 //This test will be failed.
 @Test
 public void method1() {
 System.out.println("This is method 1");
 throw new RuntimeException();
 }

@Test(dependsOnMethods = { "method1" })
 public void method2() {
 System.out.println("This is method 2");
 }
}

 

output:

[RemoteTestNG] detected TestNG version 6.14.3
This is method 1
FAILED: method1
SKIPPED: method2
===============================================
 Default test
 Tests run: 2, Failures: 1, Skips: 1
===============================================

===============================================
Default suite
Total tests run: 2, Failures: 1, Skips: 1
===============================================


 

Subscribe to Newsletter
Get Latest Posts, Articles and Learning Lessons on Software Testing and Test Automation
You can unsubscribe at any time

Comments are closed.