TestNG Execution Sequence Control Example

TestNG can help you to run and manage automation tests easily. It provides a lot of methods to control the test method execution order, this article will show you how to do it.

1. Set Preserve-Order In Testng.xml To Control Class Execution Sequence.

  1. preserve-order is used to control the execution sequence of all test classes in a test case.
  2. Its default value is true which means all test classes under the test case are executed in the defined sequence.
  3. In below example, the test class execution order should be ClassC —> ClassA —> ClassB.
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    <suite name="suite1">
        <test name="test" preserve-order="true">
            <classes >
                <class name="com.code-learner.ClassC">
                <class name="com.code-learner.ClassA">
                <class name="com.code-learner.ClassB">
            </classes >
  4. There may be multiple Test methods (annotated by @Test) in a test class, The execution order of these methods is not controlled by the preserve-order value.
  5. The default execution sequence of the test method is decided by the method name’s first letter in ascending order.

2. Test Method Execute Sequence Control.

  1. If you have multiple test methods in a TestNG class, you can define the test method execute order using the priority attribute value. The value starts from 0, the bigger priority value’s test method will be executed later.
  2. In below example code, the test method execution order is method3 —> method1 —> method2.
    @Test(priority = 0)
    public void method3() {
    @Test(priority = 1)
    public void method1() {
    @Test(priority = 2)
    public void method2() {
  3. If you do not set a priority value, the test method will be executed in order by the method name first letter ascending.
  4. You can also use the include tag in the testng.xml file to specify the test method and their order.
  5. In the below example, the test method executes order is method2 —> method1.
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
        <test name="test1">
                <class name="com.code-learner.TestClass">
                        <include name="method2" />
                        <include name="method1" />

3. Question & Answer.

3.1 How to make test methods run in a fixed order because all the test methods have dependencies.

  1. My test class contains several test methods, and I want to make the test methods execute in a fixed order because the test methods have dependencies. For example, test method 1 should run first, then only when test method 1 completes then test method 2 can be executed, etc. But when I run my test class, I found the test method executes in random order. I use the include tag in the testng.xml file to make the test methods execute sequentially. But it also failed, how can I fix it.
  2. You can use the dependsOnMethods attribute for the Test annotation to specify the dependencies about the different test methods. In the below example, the test method build will depend on the test method download, then only when the download method executes completely then the buid method will execute.
    @Test(dependsOnMethods = {"download"})
    public void build(){
  3. There is also another method to implement the test methods execution sequentially, that is to use the Test annotation’s priority attribute. You can read section 2 in this article to learn more.

1 thought on “TestNG Execution Sequence Control Example”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.