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

TestNG groups

  



What are Testng Groups?

In Testnggroups are used to categorize your test methods logically so you can run:

  • A specific set of tests (like "smoke", "regression", "sanity")

  • Multiple tests together, even if they are in different classes

Groups allow modular test execution, making large test suites more manageable and efficient.

TestNG Groups revolves around the concept of categorization and selective execution of test methods. They provide a flexible mechanism to label, filter, and run subsets of your total test suite based on defined criteria, without altering the structure of your test classes.The theory of TestNG Groups revolves around the concept of categorization and selective execution of test methods. They provide a flexible mechanism to label, filter, and run subsets of your total test suite based on defined criteria, without altering the structure of your test classes.


1. Categorization and Tagging Theory

At its core, a TestNG Group acts as a label or tag that can be applied to individual test methods, or even entire classes.

  • The Goal: To logically organize a large, heterogeneous set of tests into manageable categories.
  • The Mechanism: A test method is simply assigned one or more group names (e.g., "Smoke," "Regression," "Database," "UI"). A single test can belong to multiple groups.
  • Benefit: This creates a many-to-many relationship where tests are tagged based on their function, scope, or execution time.


2. Selective Execution Theory 

The primary utility of groups is to control which tests are executed during a specific test run. This control is managed through the testng.xml configuration file.

  • Inclusion: You can theoretically instruct the TestNG runner to include specific groups. For example, by specifying the "Sanity" group, the runner will only execute methods and classes tagged with "Sanity," skipping all others. This is essential for quickly verifying critical functionality.
  • Exclusion: You can also instruct the runner to exclude certain groups. For instance, you might run a full regression suite but explicitly exclude the "LongRunning" group to save time, or exclude the "Database" group if the database is temporarily unavailable.


3. Execution Scope and Filtering

Groups allow for granular filtering at different levels of the TestNG hierarchy.

  • Method Filtering: The most common use is to tag and filter individual test methods.
  • Class/Package Filtering: A group can be applied at the class or package level, meaning every test method inside that container automatically inherits that group tag. This simplifies organization when dealing with large modules.
  • Pre-defined Execution: By combining inclusion and exclusion rules in the testng.xml, a single test class file can support multiple, pre-defined test runs (e.g., a "CI-Build" run that uses the "Smoke" group, and a "Nightly" run that uses the "Regression" group).












Why Use Groups?

  • Run a subset of tests without running the entire suite
  • Reuse the same test in multiple groupings
  • Organize tests based on functionality, feature, or type (example: @smoke, @regression, @login)


Example: TestNG Groups using Java

Step 1: Create Java Test Class with Groups

Below in Test class, we have created Tests which are annotated with @Test and created groups with groups attribute.(ex: groups = {"smoke, "login"}
We can name multiple groups in groups attribute separated with comma. 

import org.testng.annotations.Test;

public class GroupTestExample {

    @Test(groups = {"smoke", "login"})
    public void loginTest() {
        System.out.println("Smoke/Login Test: Login functionality");
    }

    @Test(groups = {"regression"})
    public void profileTest() {
        System.out.println("Regression Test: Profile page validation");
    }

    @Test(groups = {"sanity", "checkout"})
    public void paymentTest() {
        System.out.println("Sanity/Checkout Test: Payment flow");
    }

    @Test
    public void generalTest() {
        System.out.println("General Test: No group assigned");
    }
}



Step 2: Define Groups in testng.xml

Below is the testng.xml file, here only smoke group is included as shown below so tests which is in smoke group will be executed. In above java class only one test is there which has smoke group, so only that test will execute.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="GroupSuite">
  <test name="SmokeGroupOnly">
    <groups>
      <run>
        <include name="smoke"/>
      </run>
    </groups>
    <classes>
      <class name="GroupTestExample"/>
    </classes>
  </test>
</suite>



Output

Smoke/Login Test: Login functionality


We can also run multiple groups:

<include name="smoke"/>
<include name="sanity"/>



We can also exclude specific groups:

<groups>
  <run>
    <exclude name="regression"/>
  </run>
</groups>



If a test case execution depends upon successful execution of group of test cases then we can also create dependencies on test cases by using 'dependsOnGroups' attribute in @Test annotation, as shown below in code snippet.

@Test(groups = "db")
public void connectToDB() {
    System.out.println("Connected to DB");
}

@Test(groups = "sanity", dependsOnGroups = "db")
public void fetchUserData() {
    System.out.println("Fetched user data after DB connection");
}



Suggested Posts:

1. Priority in TestNG
2. TestNG Annotations
3. TestNG Asserts
4. First Script in TestNG
5. Read Excel Data by Data provider