Dependency is a feature in TestNG that allows a test method to depend on a single or a group of test methods. This will help in executing a set of tests to be executed before a test method.
Tests dependency only works if the depend-on-method
is part of the same class or any of the inherited base class (i.e. while extending a class).
In this tutorial, we’ll learn about creating dependent tests in TestNG.
Table of Contents 1. Test with single test method dependency 2. Test with multiple test methods dependencies 3. Inherited dependency test 4. Test that depends on a group
1. Writing test with single test dependency
Use dependsOnMethods
to create a sample test method that depends on another test method of the same class.
public class DependentTestExamples { @Test(dependsOnMethods = { "testTwo" }) public void testOne() { System.out.println("Test method one"); } @Test public void testTwo() { System.out.println("Test method two"); } }
The preceding test class contains two test methods which print a message name onto the console when executed. Here, test method testOne
depends on test method testTwo
.
This is configured by using the attribute dependsOnMethods
while using the Test annotation.
Let’s run the tests now.
Test method two Test method one PASSED: testTwo PASSED: testOne
In the above test result you can see the message Test method two printed before the Test method one message. This shows that the testOne
method got executed after testTwo
as it depends on testTwo
.
2. Writing test with multiple tests dependencies
Sometimes it may be required for a test method to depend upon multiple other methods. This feature is very well supported by TestNG as part of the dependency support.
public class DependentTestExamples { @Test(dependsOnMethods = { "testTwo", "testThree" }) public void testOne() { System.out.println("Test method one"); } @Test public void testTwo() { System.out.println("Test method two"); } @Test public void testThree() { System.out.println("Test method three"); } }
The preceding test class contains three test methods which print a message name onto the console when executed. Here test method testOne
depends on test methods testTwo
and testThree
. This is configured by using the attribute dependsOnMethods while using the Test annotation.
Let’s run the test now.
Test method three Test method two Test method one PASSED: testThree PASSED: testTwo PASSED: testOne
By looking at the console message we can see that methods testTwo
and testThree
got executed before testOne
.
3. Inherited dependency test
Till now we have seen samples in which the dependent test methods were part of the same class. Dependency on test methods can only be mentioned for test methods that belong to the same class or any of the inherited base classes.
Now, let’s see how TestNG executes the test methods when the dependent tests are part of the inherited base class.
public class ParentClassTest { @Test(dependsOnMethods = { "testTwo" }) public void testOne() { System.out.println("Test method one"); } @Test public void testTwo() { System.out.println("Test method two"); } } public class DependentTestExamples extends ParentClassTest { @Test(dependsOnMethods = { "testOne" }) public void testThree() { System.out.println("Test three method in Inherited test"); } @Test public void testFour() { System.out.println("Test four method in Inherited test"); } }
The preceding test class contains two test methods which print a message name onto the console when executed. Here test method testThree
depends on test method testOne
. This is configured by using the attribute dependsOnMethods
while using the Test annotation.
Let’s run the test now.
Test four method in Inherited test Test method two Test method one Test three method in Inherited test PASSED: testFour PASSED: testTwo PASSED: testOne PASSED: testThree
As you can see from the test results the sequence of execution is testFour
, testTwo
, testOne
, and lastly, testThree
. As testThree
depends on testOne
and on testTwo
, TestNG executes all the test methods based on the dependency and finally the respective test method.
4. Tests depend on groups
Similar to dependent tests, TestNG also allows tests to depend on groups. This makes sure that a group of test methods gets executed before the dependent test method.
public class DependentTestExamples { @Test(dependsOnGroups = { "test-group" }) public void groupTestOne() { System.out.println("Group Test method one"); } @Test(groups = { "test-group" }) public void groupTestTwo() { System.out.println("Group test method two"); } @Test(groups = { "test-group" }) public void groupTestThree() { System.out.println("Group Test method three"); } }
The preceding test class contains two test methods which print a message name onto the console when executed. Here, test method testOne
depends on test method testTwo
. This is configured by using the attribute dependsOnMethods while using the Test annotation.
Let’s run the tests now.
Group Test method three Group test method two Group Test method one PASSED: groupTestThree PASSED: groupTestTwo PASSED: groupTestOne
In case you need a test method that exists in a separate class; you can achieve this by assigning the said test method to a group and configuring the dependent test method to be dependent on that group.
That’s all related to dependent tests in TestNG. Let me know if you have any queries.
Happy Learning !!
Coder
Can we define dependency between 2 different Test Classes.
Dasagriva manu
Thanks for explaining TestNG dependency concept with such clarity
Himansu
Hi Lokesh,
Similar to dependencyOnXXXX we can also control the order of execution using priority and this works even between 2 completely separated class and which can further fine tuned by placing them in the same group or separate.
Class A{
@Test( priority = 2 )
M1(){}
}
Class B{
@Test(priority = 1)
M2(){}
}
kavya
Is there any possible way to skip test and also other tests depend upon this test in testng