TestNG Execution Sequence Control Example

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

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

preserve-order is used to control the execution sequence of all test classes in test case. It’s default value is true which means all test classes under the test case are executed in the definition sequence.

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 >

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. The default execution sequence of test method is decided by the method name’s first letter in ascending order.

2. Test Method Execute Sequence Control.

If you have multiple test method in a TestNG class, you can define the test method execute order use priority attribute value. The value start from 0, bigger priority value’s test method will be executed later.

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() {

If you do not set priority value, the test method will be executed order by the method name first letter ascending.

You can also use include tag in testng.xml file to specify the test method and their order. In below example, the test method execute 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" />