TestNg Dependencies

Sometime we need to run test methods in certain order, As we need to run a test method always run after some test method. TestNg provide two ways to accomplish this either with annotation or XML suite.

Dependencies with annotations.

dependsOnMethods or dependsOnGroup attribute are used on @Test annotation to accomplish execution of dependent test.

There are two kinds of dependencies:

Hard dependencies:  All the methods you depend on must have run and succeeded for you to run. If at least one failure occurred in your dependencies, you will not be invoked and marked as a SKIP in the report.

Soft dependencies: You will always be run after the methods you depend on, even if some of them have failed. This is useful when you just want to make sure that your test methods are run in a certain order but their success doesn't really depend on the success of others. A soft dependency is obtained by adding "alwaysRun=true" in your @Test annotation.

package com.test;

import org.junit.Assert;
import org.testng.annotations.Test;

public class DependenciesTest {
        public void testMethod1() {       
           System.out.println("Test Method1 executed");        
        public void testMethod2() {       
           System.out.println("Test Method2 executed");
        public void testMethod3() {       
           System.out.println("Test Method3 executed");         
        @Test(dependsOnMethods={"testMethod3"},alwaysRun = true)
        public void testMethod4() {       
           System.out.println("Test Method4 always executed");         

When above class executed first test method "testMethod1" executed after that "testMethod2" executed and "testMethod3" not executed due to "testMethod2" failure at last "testMethod4" executed as it marked as "alwaysRun = true"


Test Method1 executed
Test Method2 executed
Test Method4 always executed
PASSED: testMethod1
PASSED: testMethod4
FAILED: testMethod2     


  1. But can we use such a mechanism where a test method will run if either of the dependent methods pass? I mean, if test13() depends on test11() and test12() & either of the methods pass will test13() be run?

  2. Is there any way to run a test method if either of the dependent methods pass? I mean, if test13() depends on test11() & test13 and either of the test11 or test12 passed, test12 will be run? Is is possible in testng?


Leave your comments, queries, suggestion I will try to provide solution