The concept of Dependent Tests in TestNG is a mechanism used to establish a sequential, conditional relationship between test methods. It ensures that a specific test method (the dependent) is executed only if one or more other test methods (the dependencies) have successfully completed their run.
The Core Concept: Conditional Execution
- Goal: To prevent subsequent tests from running when a critical prerequisite test has failed.
- Mechanism: You use a specific TestNG setting (an annotation parameter) on a test method to declare which other test methods it depends on.
What Are Dependent Tests in TestNG?
TestNG allows you to create test dependencies, meaning you can specify that:
- A test method depends on another method or group
- If the depended method fails, is skipped, or is not run, the dependent method will also be skipped
| Type | Annotation Attribute | Description |
|---|---|---|
| Method Dependency | dependsOnMethods | Test method runs only if other method(s) pass |
| Group Dependency | dependsOnGroups | Test method runs only if group(s) pass |
- Here, login() depends upon successful execution of launchApp() test case. if launchApp will execute successfully then only login() will execute otherwise, it will be skipped.
- searchproduct(), depends upon successful execution of login() test case. if login() will execute successfully then only searchproduct() will execute otherwise, it will be skipped.
- logout(), depends upon successful execution of searchproduct() test case. if searchproduct() will execute successfully then only logout() will execute otherwise, it will be skipped.
import org.testng.annotations.Test; public class DependentTestExample { @Test public void launchApp() { System.out.println("App launched successfully."); } @Test(dependsOnMethods = "launchApp") public void login() { System.out.println("Logged in successfully."); } @Test(dependsOnMethods = {"login"}) public void searchProduct() { System.out.println("Product searched successfully."); } @Test(dependsOnMethods = {"searchProduct"}) public void logout() { System.out.println("Logged out successfully."); } }
Output
App launched successfully. Logged in successfully. Product searched successfully. Logged out successfully.
Note: If a Test Fails then searchProduct() and logout() will be skipped.
import org.testng.annotations.Test; public class GroupDependencyExample { @Test(groups = "database") public void connectToDB() { System.out.println("Database connected."); } @Test(groups = "database") public void fetchData() { System.out.println("Data fetched from DB."); } @Test(dependsOnGroups = "database") public void generateReport() { System.out.println("Report generated using DB data."); } }
| Attribute | Description |
|---|---|
| alwaysRun = true | Run test even if dependency fails |
| enabled = false | Disable the test |
| dependsOnMethods | Declare method dependencies |
| dependsOnGroups | Declare group dependencies |
alwaysRun
@Test(dependsOnMethods = "login", alwaysRun = true) public void sendEmail() { System.out.println("Email sent (even if login fails)."); }
Suggested Posts:
1. Groups in TestNG
2. TestNG Annotations
3. TestNG Asserts
4. First Script in TestNG
5. Read Excel Data by Data provider