Dependency Test in TestNG
Dependency test in TestNG will discuss about how a test can dependent on other tests or group of tests. This feature help in executing a set of tests to be executed before a test method. Method dependency only works if the depend-on-method is part of the same class or any of the inherited base class.
Test depends on Single Method:
A test can dependent on a single method or more than a single method. In this section we will see how a test depends on a single method.
Below is the sample program:
package com.java.testng.youtube; import org.testng.annotations.Test; public class DependsOnSingleTest { @Test(dependsOnMethods={"testTwo"}) public void testOne() { System.out.println("This method depends on testTwo"); } @Test public void testTwo() { System.out.println("This method should execute before testOne"); } }
In the above program, we have two test methods called “testOne”,”testTwo” and testOne depends on testTwo, this can be achieved by one of the @Test attributes called “dependsOnMethods” as shown in the program. When you set this attribute, then it will look for the dependent method to execute first before the dependency test. If you execute the above program testTwo will execute first and then testOne.
Test depends on Multiple Methods:
As discussed in the previous section a method can depends on single method or multiple methods. In this section we will see how a test can depends on multiple methods.
Below is the sample program:
package com.java.testng.youtube; import org.testng.annotations.Test; public class DependsOnSingleTest { @Test(dependsOnMethods={"testTwo","testThree"}) public void testOne() { System.out.println("This method depends on testTwo and testThree"); } @Test public void testTwo() { System.out.println("testTwo method should execute before testOne"); } @Test public void testThree() { System.out.println("testThree method should execute before testOne"); } }
In the above program, we have three test methods called “testOne”,”testTwo” and “testThree” and testOne depends on testTwo,testThree, this can be achieved by one of the @Test attributes called “dependsOnMethods” as shown in the program. When you set this attribute, then it will look for the dependent method to execute first before the dependency test. If you execute the above program testTwo and testThree will execute first and then testOne. And we need to use comma to separate the test methods in the dependsOnMethods attribute.
Test that depends on its ParentClass Methods:
In the previous sections we have seen that dependency test and dependent tests are in the same class. But in this scenario dependency and dependent tests are in a seperate classes and there should be a relation between these two classes to achieve this functionality. Means, dependency test will be in the child class and dependent method should be parent class and child class should extend the parent class.
Below are the sample programs:
Child Class:
package com.java.testng.youtube; import org.testng.annotations.Test; public class ChildClassForTestDependency extends ParentClassForTestDependency { @Test(dependsOnMethods={"parentTestOne"}) public void childTestOne() { System.out.println("This method depends on parentTestOne which is in another class"); } @Test public void childTestTwo() { System.out.println("This is childTestTwo"); } }
Parent Class:
package com.java.testng.youtube; import org.testng.annotations.Test; public class ParentClassForTestDependency { @Test public void parentTestOne() { System.out.println("This method is the base for childTestOne method which is in another class"); } }
In the above, we have two classes called ChildClassForTestDependency and ParentClassForTestDependency. ChildClassForTestDependency methods are depends on ParentClassForTestDependency methods and this can be achieved using the inheritance concepts that child class will extend the parent class then child class can depends on the methods which are available in the parent class.
childTestOne is dependent on parentTestOne in the above example. Once the parentTestOne executes then the childTestOne will execute.
This way we can make tests dependent on each other to control the execution flow while executing the test suite.
Please watch the youtube video for better understanding.