Hello Folks,
We have learnt in previous posts regarding establishing relationship between test methods. You can go through them below:
Dependency in TestNG – Creating Dependency Among Test Methods – DependsOnMethod
Dependency in TestNG – Creating Dependency Among Test Methods – DependsOnGroup
In this post, we will learn how can we use regular expressions with DependsOnGroup.
Regular expression helps you in defining a pattern for similar items. If your test is dependent on multiple groups of tests, either you can pass each group name in dependsOnGroups array or pass regular expressions if group names follows a pattern. We will see examples below:
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"); } }
Output:
[java]
[RemoteTestNG] detected TestNG version 6.14.2
Signed Up
Registration Was successful
Logged In
Log In was successful.
purchased Something
PASSED: signUp
PASSED: verifyRegistration
PASSED: logIn
PASSED: verifyLogIn
PASSED: purchaseSomething
===============================================
Default test
Tests run: 5, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 5, Failures: 0, Skips: 0
===============================================
[/java]
Above way will be complex if you more number of groups or adding or deleting a new group. Every time you may need to edit group names in dependsOnGroups array. But if you name group in some pattern, you can use regular expressions as shown below:
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"); } }
Output:
[java]
[RemoteTestNG] detected TestNG version 6.14.2
Signed Up
Registration Was successful
Logged In
Log In was successful.
purchased Something
PASSED: signUp
PASSED: verifyRegistration
PASSED: logIn
PASSED: verifyLogIn
PASSED: purchaseSomething
===============================================
Default test
Tests run: 5, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 5, Failures: 0, Skips: 0
===============================================
[/java]
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium