Priority in TestNG with multiple classes

Last Updated : 23 Jul, 2025

In TestNG, the priority attribute allows you to control the execution order of the test methods within the class. However, when we working with the multiple test classes, simply using the priority is not enough to manage execution order between the classes. In this case, the testing.xml configuration file has become essential, as it defines the execution order of the classes.

Approach to Set Priority in TestNG with multiple classes

To control the execution order of the methods across the multiple classes and also manage the priorities within each class, follow the below steps:

  • Use the priority attribute within each class. The priority attribute is ensure that the test methods within the each class are executed in defined order. Lower priority values indicate the higher execution precedence.
  • Define class execution order in testng.xml: Since priority works only within the class, we need to specify order in which the classes are executed in testng.xml file. TestNG will run classes in order they appear in file, ensuring that all methods of one class execute before begins the next class.

Example: Priority in TestNG with Multiple Classes

In this testNG example, we will demonstrate the how to achieve desired test execution sequence where:

  • All the methods of first class are executed before the moving on second class.
  • Method priorities within the each class are respected.

1. ClassA.java

Java
import org.testng.annotations.Test;

public class ClassA {

    @Test(priority = 1)
    public void testA1() {
        System.out.println("ClassA - Test A1");
    }

    @Test(priority = 2)
    public void testA2() {
        System.out.println("ClassA - Test A2");
    }

    @Test(priority = 3)
    public void testA3() {
        System.out.println("ClassA - Test A3");
    }
}

2. ClassB.java

Java
import org.testng.annotations.Test;

public class ClassB {

    @Test(priority = 1)
    public void testB1() {
        System.out.println("ClassB - Test B1");
    }

    @Test(priority = 2)
    public void testB2() {
        System.out.println("ClassB - Test B2");
    }

    @Test(priority = 3)
    public void testB3() {
        System.out.println("ClassB - Test B3");
    }
}

3. testing.xml: Controlling Class Execution Order

To ensure that the ClassA is run entirely before the ClassB, we will define class execution order in testing.xml file as shown in below:

XML
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite1">
    <test name="Class Execution Order">
        <classes>
            <class name="ClassA"/>
            <class name="ClassB"/>
        </classes>
    </test>
</suite>

Output

TestNG
Output

Explanation:

  • In the above example, ClassA and ClassB in testing.xml file. This is ensure that all the methods in ClassA that means based on the priority will be execute first and after followed by all the methods in the ClassB.
  • In the above output, ClassA runs completely first and only after that the ClassB is executed.

The above approach is useful when we have the multiple test classes and we want to ensure that execution of the methods in one class is completed before the starting next class, while maintaining the method-level prioritization within the each class.

Conclusion

In conclusion, by using the combination of priority attribute within the classes and specifying a class execution order in the testng.xml, we will control both order of the test methods within the class and sequence in which entire classes are to be executed. This approach is ensure that all the test methods in the one class are to be executed before the moving on another class, provide the fine-grained control over the test execution in the multi-class scenario. This method is especially useful when the tests are need to be follow the specific sequence due to the dependencies between the different classes.

Comment