18
Mar

Creating “dependsOnGroups” dependency via regular expressions

One can define a dependency between groups using “dependsOnGroups()” with regular expression as follow:
import org.testng.annotations.Test;

public class MultipleGroups {

// @Parameters({ “optional-value” })
// @Test
// public void optionTest(@Optional(“optional value”) String value) {
// System.out.println(“This is: ” + value);
// }

// Explicitly failing method on which Test2 is dependent
@Test(groups={“GroupA”})
public void Test1() {
System.out.println(“I am Test1”);
}

@Test(groups={“GroupB”})
public void Test2() {
System.out.println(“I am Test2”);
}

// Test2 will not run as Test1 has failed. Test2 will be marked as skipped.
@Test(dependsOnGroups={“Group.*”}, alwaysRun = true)
public void Test3() {
System.out.println(“I am Test3”);
}

}

Output:
——-
===============================================
Default test
Tests run: 3, Failures: 0, Skips: 0
===============================================

Note: this is not applicable for “dependsOnMehtods” and causes cyclic dependencies.