top of page

TestNG Tutorial

Dependent Tests In TestNG

What are dependencies In TestNG ?

In TestNG there's a feature that allows a test method to depend on a single or a group of test methods and also allows multiple dependencies. A test or a set of tests are executed before a particular test method, if dependency is applied on it. ​ Dependency is of two kinds as :
  1. Method Dependency : Attribute 'dependsOnMethods' is used.

  2. Group Dependency : Attribute 'dependsOnGroups' is used.

Dependency can be applied in following ways :
Single Dependency :

Single Method Dependency : @Test(dependsOnMethods = { "methodName" }) public void f() { } Single Group Dependency : @Test(dependsOnGroups = { "groupName" }) public void f() { }

An example of method dependency :

package testNGexamples; import org.testng.annotations.Test; public class Single_MethodDependency { @Test(dependsOnMethods = { "dependingFunction" }) public void dependentFunction() { System.out.println("This will execute, if dependingFunction() is passed "); } ​ @Test public void dependingFunction() { System.out.println("This will execute first "); } }


Output :

[RemoteTestNG] detected TestNG version 7.4.0 This will execute first . This will execute, if dependingFunction() is passed . PASSED: dependentFunction PASSED: dependingFunction =============================================== Default test Tests run: 2, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================

An example of group dependency :

package testNGexamples; import org.testng.annotations.Test; public class Single_GroupDependency { @Test(dependsOnGroups = { "depends on group" }) public void dependentFunction() { System.out.println("This will execute if dependent group is executed successfully "); } ​ @Test(groups = { "depends on group" }) public void dependendingFunction1() { System.out.println("dependingFunction1() of a group will execute before the dependentFunction() "); } ​ @Test(groups = { "depends on group" }) public void dependendingFunction2() { System.out.println("dependingFunction2() of a group will execute before the dependentFunction() "); } }


Output :

[RemoteTestNG] detected TestNG version 7.4.0 dependingFunction1() of a group will execute before the dependentFunction() . dependingFunction2() of a group will execute before the dependentFunction() . This will execute if dependent group is executed successfully . PASSED: dependendingFunction2 PASSED: dependentFunction PASSED: dependendingFunction1 =============================================== Default test Tests run: 3, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 3, Passes: 3, Failures: 0, Skips: 0 ===============================================


Multiple Dependencies :

Multiple Method Dependencies : @Test(dependsOnMethods = { "methodName1", "methodName2","methodName3" }) public void f() { } Multiple Group Dependencies : @Test(dependsOnGroups = { "groupName1", "groupName2","groupName3" }) public void f() { }


An example of multiple dependencies :

package testNGexamples; import org.testng.annotations.Test; public class MultipleDependencies { @Test public void test1() { System.out.println("test1() will execute before test2() : "); } ​ // Multiple method dependency... @Test(dependsOnMethods = { "test1", "test4" }) public void test2() { System.out.println("test2() will execute, if test1() and test4() is executed successfully : "); } ​ @Test(groups = { "test group 1" }) public void test3() { System.out.println("test3() of test group 1 will execute before test6() : "); } ​ @Test public void test4() { System.out.println("test4() will execute before test2() : "); } ​ @Test(groups = { "test group 1" }) public void test5() { System.out.println("test5() of test group 1 will execute before test6() : "); } ​ // Multiple group dependency... @Test(dependsOnGroups = { "test group 1", "test group 2" }) public void test6() { System.out.println("test6() will execute, if test group 1 and test group 2 is executed successfully : "); } ​ @Test(groups = { "test group 2" }) public void test7() { System.out.println("test7() of test group 2 will execute before test6() : "); } ​ @Test(groups = { "test group 2" }) public void test8() { System.out.println("test8() of test group 2 will execute before test6() : "); } }

Output :

[RemoteTestNG] detected TestNG version 7.4.0 test1() will execute before test2() : test3() of test group 1 will execute before test6() : test4() will execute before test2() : test5() of test group 1 will execute before test6() : test7() of test group 2 will execute before test6() : test8() of test group 2 will execute before test6() : test2() will execute, if test1() and test4() is executed successfully : test6() will execute, if test group 1 and test group 2 is executed successfully : PASSED: test8 PASSED: test6 PASSED: test3 PASSED: test1 PASSED: test7 PASSED: test4 PASSED: test5 PASSED: test2 =============================================== Default test Tests run: 8, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 8, Passes: 8, Failures: 0, Skips: 0 ===============================================


Inherited Dependency :
Create a different class of test methods that have to be used for dependencies. Now use 'extends' keyword to inherit that class and then add dependencies as explained above. ​ Dependency Using .xml : Dependencies are added in .xml by using dependencies tag :

<groups> <dependencies> <group name="dependent groupName" depends-on="depending groupName"></group> </dependencies> </groups>

An example of .xml dependencies :
.xml file :

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Test-Suite"> <test name="Group Test"> <groups> <dependencies> <group name="login" depends-on="openbrowser"></group> <group name="viewprofile" depends-on="login"></group> <group name="logout" depends-on="viewprofile"></group> </dependencies> </groups> <classes> <class name="testNGexamples.DependencyInXML"></class> </classes> </test> </suite>

TestNG class :

package testNGexamples; import org.testng.annotations.Test; public class DependencyInXML { @Test(groups = { "viewprofile" }) public void viewProfile() { System.out.println("Profile Has Been Viewed :"); } ​ @Test(groups = { "openbrowser" }) public void openBrowser() { System.out.println("Open Browser :"); } ​ @Test(groups = { "login" }) public void logIn() { System.out.println("Log In First :"); } ​ @Test(groups = { "logout" }) public void logOut() { System.out.println("Log Out The Session :"); } }

Output :

[RemoteTestNG] detected TestNG version 7.4.0 Open Browser : Log In First : Profile Has Been Viewed : Log Out The Session : =============================================== Test-Suite Total tests run: 4, Passes: 4, Failures: 0, Skips: 0 ===============================================

Refer next page Parallel Execution Of Tests
bottom of page