Dependency is an feature provided by TestNG which allows test methods to depend on a single or a group of test method. Suppose we have a situation when executing current method, we have a prerequisite to execute another method. At this situation , we can go for DependsonMethod feature.
TestNG DependsOnMethods Example 1 :
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import org.testng.annotations.Test; public class DependsOnMethod { @Test(dependsOnMethods = { "testScript2" }) public void testScript1() { System.out.println("This is script 1"); } @Test public void testScript2() { System.out.println("This is script 2"); } } |
Output :
TestNG-DependsOnMethod-Example
When we execute the above program, since we have used
dependsonmethod attribute with Test annotation, testscript1 has
dependency with testscript2. Hence testScript2 will be executed first,later testscript1 will be executed.
TestNG DependsOnMethods Example 2 :
1 2 3 4 5 6 7 8 9 10 11 12 |
import org.testng.annotations.Test; public class DependsOnMethod { @Test(dependsOnMethods = { "testScript2" }) public void testScript1() { System.out.println("This is script 1"); } @Test public void testScript2() { System.out.println("This is script 2"); throw new RuntimeException(); } } |
Output :
TestNG-DependsOnMethod-Exception
When we execute the above program,testScript2 will be failed which contains RunTimeException. Hence TestNG will skip the testScript1 method.