Dependency Attribute With @Test Annotation: As we are going in the learning of the TestNG automation framework, we are learning new and exciting topics as well. In this post, we are going to discuss another important feature of TestNG, which is Dependency.
As we are using TestNG as a unit testing framework, and in that, we don’t want the result of one test method does not affect other test methods. But sometimes we came across some situations where one test method to be dependent on other tests.
For Example:
You want to send a mail to your dear one and for that, if we take a look at the test case, then that should be something like this:
- Launch browser
- Open Gmail
- Sign in to Your Account
- Send email
If you see that all the operations should be performed in the mentioned order, otherwise, your test script fails. So in the above example, each test case is dependent on each other. If you change the functionality, then the required functionality will not work.
In this case, we need to maintain dependency between the test method or between scenarios in some order and execute than on the result of the previous method so that we can save time as well.
To Handle with Such scenarios TestNg provides two attributes dependsOnMethod and dependsOnGroup. You can use these two attributes with @test annotation to achieve the dependency between the tests.
How to Use Dependency Attribute On Method Level
Things to Remember about dependsOnMethod:
- If you are executing the entire suite or class, always the parent methods will run first, and after that, only the dependent methods will be executed.
- If you are only running the dependent test, then also the parent method executed first and later dependent test will be executed.
- If the parent methods failed, then the dependent test will not run, and it will mark as skipped.
Below is the sample code to understand how dependsOnMethod is working at the method level
package Dependecy; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class MultiLevelDependency { @BeforeTest public void launchBrowser() { System.out.println(“browser Launched.”); } @Test public void loadFacebookURL() { System.out.println(“Facebook URL loaded.”); } /* * registerOnFacebook depends on loadFacebookURL */ @Test(dependsOnMethods = {“loadFacebookURL”}) public void registerOnFacebook() { System.out.println(“Register on Facebook.”); } /* * postStatusOnFacebook depends on registerOnFacebook */ @Test(dependsOnMethods = {“registerOnFacebook”}) public void postStatusOnFacebook() { System.out.println(“Post an status on Facebook.”); } }
How dependsOnGroup Working In TestNG?
Above we have seen how we can use the dependency on the method level, and now we are going to learn how to use dependency on the group level. Because when we are doing
testing according to the behaviour, we are conducting different types of testing like smoke testing, integration testing, sanity testing, regression testing.
As an automation tester, when we get a build first, we do some testing, and if the smoke testing is pass, then we start another testing. Similarly, for doing smoke testing, we have a make a group for that suppose smoke group. If all the methods of the smoke group are passed, then only, we should continue our testing process. In that case, we can use the dependsOnGroup attribute.
Some points to remember about dependsOnGroup:
- If you ware executing the entire suite or class, always the parent groups will execute first, and after that, only the dependent groups will be run.
- If you are only running the dependent group, then also the parent groups executed first and later dependent group will be executed.
- If in the parent group and test methods got failed, then the dependent group will not run, and it will mark as skipped.
Sample Program:
package Dependency; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class DependsOnGroupsExample { // Test method belong to preSetupTestA @Test(groups= {"preSetupTestA"}) public void methodA() { System.out.println("MethodA"); } //Test method belong to preSetupTestA @Test(groups= {"preSetupTestA"}) public void methodB() { System.out.println("MethodB"); } // Test method belong to preSetupTestB @Test(groups= {"preSetupTestB"}) public void methodC() { System.out.println("MethodC"); } // Test method belong to preSetupTestB @Test(groups= {"preSetupTestB"}) public void methodD() { System.out.println("MethodD"); } // Test method which is dependent of other groups @Test(dependsOnGroups = {"preSetupTestB", "preSetupTestA"}) public void finalTest() { System.out.println("Final Test."); } }
Note: At a time, you can mention one group or multiple groups. Commas can separate those groups.
How to Use Regular Expression With DependsOnGroup?
During the use of dependsOnGroups, we have seen on the dependent method we need to mention all the depends on group name there. But if the group’s name on a specific pattern, then we can say the depends on group names with regular expression. So that no need to mention all the group name one by one.
For Example:
package Dependecy; import org.testng.annotations.Test; public class RegularExpressionsInDependsOnGroups { // Test belong to Group registration @Test(groups = "registration", priority= 1) public void signUp() { System.out.println("Signed Up"); } // Test belong to Group registrationStatus @Test(groups = "registrationStatus", priority= 2) public void verifyRegistration() { System.out.println("Registration Was successful"); } // Tests belong to group login @Test(groups = "login", priority= 3) public void logIn() { System.out.println("Logged In"); } // Tests belong to group loginStatus @Test(groups = "loginStatus", priority= 4) public void verifyLogIn() { System.out.println("Log In was successful."); } // This test is dependent on all above groups. You need to mention all group names explicitly here. @Test(dependsOnGroups = { "registration", "login", "registrationStatus", "loginStatus" }) public void purchaseSomething() { System.out.println("purchased Something"); } }
If you saw the above code, then you can say that we have mentioned all the group names, so its become complicated when there is a number of groups or adding or deleting a new group.
At the time of adding or deleting, you need to change the group names, but the same thing we can handle easily with the pattern.
package Dependecy; import org.testng.annotations.Test; public class RegularExpressionsInDependsOnGroups { // Test belong to Group registration @Test(groups = "userAccess_registration", priority= 1) public void signUp() { System.out.println("Signed Up"); } // Test belong to Group registrationStatus @Test(groups = "userAccess_registrationStatus", priority= 2) public void verifyRegistration() { System.out.println("Registration Was successful"); } // Tests belong to group login @Test(groups = "userAccess_login", priority= 3) public void logIn() { System.out.println("Logged In"); } // Tests belong to group loginStatus @Test(groups = "userAccess_loginStatus", priority= 4) public void verifyLogIn() { System.out.println("Log In was successful."); } // This test is dependent on all above groups. Since we have followed a naming pattern in group names, we can use regular expression with // dependsOnGroups. @Test(dependsOnGroups = { "userAccess_.*" }) public void purchaseSomething() { System.out.println("purchased Something"); } }
Types Of Dependency On TestNG
Earlier in this post, we have seen how we can create a dependency between methods. Now we are learning various types of dependency on testNG & how to use that in your testing framework.
In TestNG, we have two types of Dependency:
- Hard Dependency
- Soft Dependency
Hard Dependency: In this type of dependency, if all the dependence on a method passed, then only the dependent on the method can run. Otherwise, the depend on the method invoked and marked as SKIP in the testNG report.
Soft Dependency: In this type of dependency, if some of them depend on methods got failed, then also the dependent method will run. That means it does not rely on the result of the depends on methods. We can get the soft dependency by using the “alwaysRun=true” attribute with the @test annotation.
Hard Dependency On TestNG
When we have implemented dependsOnMethod, we have seen if the parent method failed, the dependent method got skipped, but that time, we don’t know that the skipped method is a failed method or not in the test.
For Example:
package Dependecy; import org.testng.Assert; import org.testng.annotations.Test; public class HardDependeny { // Explicitly failing method on which Test2 is dependent @Test public void Test1() { System.out.println("I am Test1"); Assert.fail(); } // Test2 will not run as Test1 has failed. Test2 will be marked as skipped. @Test(dependsOnMethods= {"Test1"}) public void Test2() { System.out.println("I am Test2"); } }
Soft Dependency On TestNG
If you want to run, the dependent method always runs irrespective of the result of depends on the method. Then that type of dependency we called soft dependency, and we can achieve that by adding the alwaysRun=true attribute in the dependent method.
Sample Program:
package Dependecy; import org.testng.Assert; import org.testng.annotations.Test; public class HardDependeny { // Explicitly failing method on which Test2 is dependent @Test public void Test1() { System.out.println("I am Test1"); Assert.fail(); } // Test2 will not run as Test1 has failed. Test2 will be marked as skipped. @Test(dependsOnMethods= {"Test1"}, alwaysRun= true) public void Test2() { System.out.println("I am Test2"); } }
ignoreMissingDependencies In TestNG
We have seen the hard dependency and soft dependency we can use with the existed depends on the method, but the thing about a scenario like:
Scenario 1:
How will you run your test, which depends on other tests and that tests do not always exist or need to execute based on some conditions?
package Dependency; import org.testng.annotations.Test; public class HardDependeny { /* * This test method depends on another test method named "Test1" which does not exist. */ @Test(dependsOnMethods= {"Test1"}) public void Test2() { System.out.println("I am Test2"); } }
When you run the above class that time you will get an exception where it is mentioned that “depends on the nonexistent method.”
Scenario 2:
How you will run your test, which depends on another test, which are mark false for “enabled” attribute.
package Dependecy; import org.testng.annotations.Test; public class HardDependeny { @Test(enabled= false) public void Test1() { System.out.println("I am Test1"); } /* * This test method depends on another test method named "Test1" which is not enabled. */ @Test(dependsOnMethods= {"Test1"}) public void Test2() { System.out.println("I am Test2"); } }
In the Second scenario also you can say that you are getting an exception like below:
But, in this case, you can use the “ignoreMissingDependencies” attribute, So that you can run your tests even the dependencies are missing or disabled.
package Dependecy; import org.testng.annotations.Test; public class HardDependeny { @Test(enabled= false) public void Test1() { System.out.println("I am Test1"); } /* * This test method depends on another test method named "Test1" which is not enabled. * Setting ignoreMissingDependencies as true will make it soft dependency. */ @Test(dependsOnMethods= {"Test1"}, ignoreMissingDependencies= true) public void Test2() { System.out.println("I am Test2"); } }
TestNG Dependson Annotation Simple Program: How TestNG Dependson Annotation Works Selenium Example Program?
package com.selenium.TestNG; import org.testng.Assert; import org.testng.annotations.Test; public class DependsonTest { @Test public void login() { System.out.println("Login"); } @Test(dependsOnMethods="login") public void search() { System.out.println("Search"); Assert.assertEquals("Manoj", "Monoj"); } @Test(dependsOnMethods="search") public void advsearch() { System.out.println("Adv Search"); } @Test(dependsOnMethods="advsearch") public void logout() { System.out.println("Logout"); } }
Source: link
Leave a Reply