Skip to main content

How to Perform Dependent Test Case Discovery

Dependent tests signify that one test relies on the outcome of another. To achieve this, TestNG offers the 'dependsOnMethods' attribute within @Test annotations.

For instance, consider the code snippet in which 'SignIn()' depends on 'OpenBrowser(),' and 'LogOut()' depends on 'SignIn().'

import org.testng.annotations.Test;
public class DependsOnTest {
@Test
public void OpenBrowser() {
System.out.println("The browser is opened");
}

@Test (dependsOnMethods = { "OpenBrowser" })
public void SignIn() {
System.out.println("User has signed in successfully");
}

@Test (dependsOnMethods = { "SignIn" })
public void LogOut() {
System.out.println("The user logged out successfully");
}
}

To discover and manage dependent tests using the Test Discovery command, you can use the following syntax:

mvn test -Dmode=discover -Dplatname=win -Dframework=testng -Ddiscovery=dependent

This command will provide a Test Discovery Result that lists the tests and their dependencies, ensuring that dependent tests are executed in the correct order, such as ["Test1#SignIn,Test1#LogOut,Test1#OpenBrowser"].

Test across 3000+ combinations of browsers, real devices & OS.

Book Demo

Help and Support

Related Articles