Open In App

How to Control Test Execution Order in TestNG?

Last Updated : 23 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Controlling the order in which tests run is crucial, especially in larger software projects. By controlling the test execution order, you ensure that tests run logically. This helps in identifying real issues in the code, rather than false negatives caused by running tests out of sequence. It also makes debugging easier because failures are more likely to point to actual problems rather than issues related to test setup. In this article, We will use different methods to control Test Execution Order in TestNG.

Methods to Control Test Execution Order in TestNG

In TestNG, controlling the test execution order can be done in several ways. By using priority Attribute, Using dependsOnMethods Attribute, Using dependsOnGroups Attribute, etc. Before Understanding these methods, Let's first understand what is the default Test Execution Order in TestNG.

Default Test Order Execution

By default TestNG runs tests alphabetically by their method names. This means methods like testA() will run before testB(), regardless of their importance or dependencies.

Method 1: Using priority Attribute

In TestNG, You can specify the order of test execution by assigning a priority attribute to your test methods. Lower priority values are executed first. This means that suppose there are three test cases with priority 3, priority 2, and priority 0 respectively then the test case with priority 0 will execute first then the test case with priority 1 and priority 2.

Example of Controlling the Test Execution Order using Priority Attribute

Java
import org.testng.annotations.Test;

public class TestPriorityExample {

    @Test(priority = 1)
    public void testA() {
        System.out.println("Test A");
    }

    @Test(priority = 2)
    public void testB() {
        System.out.println("Test B");
    }

    @Test(priority = 0)
    public void testC() {
        System.out.println("Test C");
    }
}

Output:

priority_attribute
output

Method 2: Using dependsOnMethods Attribute

In testNG You can specify dependencies between test methods using dependsOnMethods. The dependent method(s) will be executed first. Suppose there is a test case testB() that depends on testA(), by using this attribute testA() method will execute first then testB() will execute.

Example of Controlling the Test Execution Order using dependsOnMethods Attribute

Java
import org.testng.annotations.Test;

public class TestDependsOnMethodsExample {

    @Test
    public void testLogin() {
        System.out.println("Login Test");
    }

    @Test(dependsOnMethods = {"testLogin"})
    public void testViewAccount() {
        System.out.println("View Account Test");
    }

    @Test(dependsOnMethods = {"testLogin"})
    public void testLogout() {
        System.out.println("Logout Test");
    }
}

Output:

depends_on_methods
dependsOnMethods

Method 3: Using dependsOnGroups Attribute

If you have grouped your test methods, you can control the execution order by specifying group dependencies.

Example of Controlling the Test Execution Order using dependsOnGroups Attribute

Java
import org.testng.annotations.Test;

public class TestDependsOnGroupsExample {

    @Test(groups = {"login"})
    public void testLogin() {
        System.out.println("Login Test");
    }

    @Test(groups = {"account"})
    public void testViewAccount() {
        System.out.println("View Account Test");
    }

    @Test(groups = {"account"}, dependsOnGroups = {"login"})
    public void testTransferFunds() {
        System.out.println("Transfer Funds Test");
    }

    @Test(groups = {"logout"}, dependsOnGroups = {"login"})
    public void testLogout() {
        System.out.println("Logout Test");
    }
}

Output:

depends_on_groups
dependsOnGroups

Conclusion

Controlling the test execution order in TestNG is vital for ensuring that your tests run in a logical sequence, especially in complex test scenarios. By using attributes like priority, dependsOnMethods, and dependsOnGroups, you can dictate the order of test execution to match the dependencies within your code. This not only helps in catching real issues but also prevents false failures due to out-of-order execution.


Next Article

Similar Reads