dependsOnMethods


Sometimes, you need your test methods to be invoked in a certain order or To make sure a certain number of test methods have completed and succeeded before running more test methods, in such cases TestNG provided dependencies facility.

In TestNG two types of Dependencies

dependsOnGroups: The list of groups this method depends on.
dependsOnMethods: The list of methods this method depends on.

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.

DependsOnMethods

Lets create a java class
import org.testng.annotations.Test;
publicclassdependonmethods {
@Test
publicvoid Testmethod1() {
System.out.println("This is Testmethod1");
}
@Test(dependsOnMethods = { "Testmethod1" })
publicvoid Testmethod2() {
System.out.println("This is Testmethod 2");
}
}

TestNG.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none">
<testname="Test1">
<classes>
<classname="dependonmethods"/>
</classes>
</test><!-- Test -->
</suite><!-- Suite -->

Results

image

Here, TestMethod2 execution is depended on TestMethod1, if TestMethod1 is PASSED TestMethod2 will execute else it will SKIP the execution of TestMethod2

Another example

Lets create a java class

import org.testng.annotations.Test;
publicclass dependonmethodfailed {
@Test
publicvoid Testmethod1() {
System.out.println("TestMethod2 will not execute because TestMethod1 is failing");
intb=1/0;
}
@Test(dependsOnMethods = { "Testmethod1" })
publicvoid Testmethod2() {
System.out.println("This is Testmethod 2");
}
}

TestNG.xml


<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none">
<testname="Test1">
<classes>
<classname="dependonmethods"/>
</classes>
</test><!-- Test -->
</suite><!-- Suite -->

Results

image

Here, TestMethod1 is failed , TestMethod2 will be skipped.


Les try with soft dependencies example- add alwaysrun=true


import org.testng.annotations.Test;
publicclass dependonmethodfailed {
@Test
publicvoid Testmethod1() {
System.out.println("TestMethod2 will not execute because TestMethod1 is failing");
intb=1/0;
}
@Test(dependsOnMethods = { "Testmethod1" },alwaysRun=true)
publicvoid Testmethod2() {
System.out.println("This is Testmethod 2");
}
}

TestNG.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none">
<testname="Test1">
<classes>
<classname="dependonmethods"/>
</classes>
</test><!-- Test -->
</suite><!-- Suite -->

Results
image
Here, TestMethod2 is executed even though TestMethod1 is failed because we are added alwaysRun=truein @Test annotationfor testMethod2.

Comments