TestNG Tutorials 63: Dependency in TestNG – Usage of Regular Expressions with DependsOnGroup

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