Open In App

Running Test Methods One After Another in TestNG?

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

TestNG is another testing framework of Java that offer promising features including test configuration, Test parameterization as well as parallel testing. One of the requirements in testing is typically the order in which test methods have to be executed especially when the result of the tests depends on previous tests. It can be done in TestNG in several ways as described below. This article will guide on how to make test methods sequential in TestNG through demonstration via codes and screenshots of the outputs.

Running Test Methods One After Another in TestNG

In TestNG, the order of test method execution can be controlled or influenced by different mechanisms, like dependsOnMethods, priority attribute, etc. If no specific order is defined, TestNG will execute test methods alphabetically by their names. For instance, a method named testLogin will run before a method named testRegister.

1. Using 'dependsOnMethods' Attribute

The options of controlling the order of test method execution in TestNG are as follows: dependsOnMethods parameter of @Test. This enables you to state which test method should be run first and others that should be run subsequently based on it. The problem with a dependent method is that when it fails, it will cause the methods dependent on it to be omitted.

Here’s a simple example to demonstrate this:

DependencyTest.java

Java
import org.testng.Assert;
import org.testng.annotations.Test;

public class DependencyTest {

    @Test
    public void testMethod1() {
        System.out.println("Executing testMethod1");
        Assert.assertTrue(true);
    }

    @Test(dependsOnMethods = {"testMethod1"})
    public void testMethod2() {
        System.out.println("Executing testMethod2");
        Assert.assertTrue(true);
    }

    @Test(dependsOnMethods = {"testMethod2"})
    public void testMethod3() {
        System.out.println("Executing testMethod3");
        Assert.assertTrue(true);
    }
}

Explanation

  • testMethod1() is the first test method and does not depend on any other method.
  • testMethod2() depends on testMethod1(), so it will be executed only after testMethod1() completes successfully.
  • testMethod3() depends on testMethod2() and will run after testMethod2() has been executed.

TestNG.xml

XML
<?xml version = "1.0" encoding = "UTF-8"?>

<suite name="Suite">
    <test name="Test Suite">
        <classes>
           
            <class name="testNG.DependencyTest">

            </class>
        </classes>
    </test>
</suite>

Output

DpendsOn


2. Using priority Attribute

Another way to control the execution order of test methods is by using the priority attribute. The lower the priority number, the earlier the method runs.

Here's an example:

PriorityTest.java

Java
import org.testng.Assert;
import org.testng.annotations.Test;

public class PriorityTest {

    @Test(priority = 1)
    public void testMethod1() {
        System.out.println("Executing PrioritytestMethod1");
        Assert.assertTrue(true);
    }

    @Test(priority = 2)
    public void testMethod2() {
        System.out.println("Executing PrioritytestMethod2");
        Assert.assertTrue(true);
    }

    @Test(priority = 3)
    public void testMethod3() {
        System.out.println("Executing PrioritytestMethod3");
        Assert.assertTrue(true);
    }
}

Explanation:

  • testMethod1() has the highest priority (priority = 1), so it will be executed first.
  • testMethod2() has a lower priority (priority = 2), so it will be executed after testMethod1().
  • testMethod3() has the lowest priority (priority = 3), so it will be executed last.

TestNG.xml

XML
<?xml version = "1.0" encoding = "UTF-8"?>

<suite name="Suite">
    <test name="Test Suite">
        <classes>
           
            <class name="testNG.PriorityTest">

            </class>
        </classes>
    </test>
</suite>

Output

PriorityTest
output

Conclusion

Generally, in TestNG, it is easy to execute test methods one after the other especially in a particular order. This can actually be done using the dependsOnMethods attribute, which creates a relationship between methods, or the priority attribute which assigns the method an order of execution. When these features are known and used successfully, it leads to better writing of test suites that are more reliable and structured. This makes it possible for your tests to run in the right order especially where the results of one test will impact on the other.


Article Tags :

Similar Reads