Test Dependency in TestNG

Reading Time: 3 minutes

Test Dependency :-

Test Dependency in TestNG is one of the useful feature in TestNG framework. It allows a test method to depend on some other method or methods. Test Dependency allows us to make one test method dependent on one or multiple other test methods.

we need to pass attribute ‘dependsOnMethods’ to ‘@Test’ annotation and then pass the names of the methods which were dependents. Below is the syntax for this:-

@Test(dependsOnMethod ={“Test_method_name”}

Test execution order:-

When it comes to Test execution order then it becomes very tedious to control execution order because there is no pre-defined order in which tests executes. For this let’s create a class with for test methods and run using TestNG.

default test execution

Thus we can see in output console there is no predefined order in which they execute.

Need of Test Dependency:-

As we can see from the above scenario there is no predefined execution order in which they can run. If there is some precondition to run any particular test method or methods only then that particular test method can run then we need to have this feature. This feature is to create a relationship among test methods. This scenario is very common when it comes to functional UI/API end to end testing.

Chaining of dependency:-

This feature is very useful in the scenario when we have to make chaining among various test methods because of the requirement. Let’s see a few examples where we can leverage this feature.

chaining od dependent methods
more than one dependent method

Dependent method failure scenario :-

When we have any method that is dependent on some other method and that method having some failure. in that case caller method skips execution as the method on which it depends is failing. This feature also gives a useful log and indicates which depends On the method facing issues and what went wrong with them. Let’s have one look at the example below as depends on method is failing and it is showing as skip in the test result.

dependent method failure

Conclusion :-

As we have seen some very useful scenarios where we can use Test Dependency in TestNG and increase clarity in functional end to end testing. But it will create pain while executing test cases in a multithreading environment. So we should use this feature when dependent test cases are must to have otherwise we should make isolated test cases that can run on the multithreaded environment.

References:-

https://testng.org/doc/documentation-main.html, https://howtodoinjava.com/testng/testng-dependent-tests-examples/

Written by 

QA Consultant