Showing posts with label TestNG Dependent Tests. Show all posts
Showing posts with label TestNG Dependent Tests. Show all posts

TestNG Dependent Tests

 



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


















Types of Dependencies in TestNG

TypeAnnotation AttributeDescription
Method DependencydependsOnMethodsTest method runs only if other method(s) pass
Group DependencydependsOnGroupsTest method runs only if group(s) pass



If a test case execution depends upon successful execution of other test cases, then in this condition we can use attribute 'dependsOnMethods' in @Test annotation.


Example Java Code for method dependency

Below is the Test class contains 4 test cases, 
  • 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.


Example Java Code for group dependency

Below there are two test cases comes in a group that is database. If all the test cases present in group database will be executed successfully then only generateReport() will execute.

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.");
    }
}




Optional Attributes

AttributeDescription
alwaysRun = trueRun test even if dependency fails
enabled = falseDisable the test
dependsOnMethodsDeclare method dependencies
dependsOnGroupsDeclare group dependencies




Example of attribute: alwaysRun

alwaysRun attribute always executes test cases. Below sendEmail() testcase has attribute alwaysRun, which means even if dependent test case fails then also this testcase sendEmail() will execute.

@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