In our Annotations in TestNG post, we understood that if there are multiple @Test annotations in a single class, they are executed alphabetically based on method name. Thankfully, TestNG provides us a better way to order our test cases rather than following a particular naming convention.
Priority Parameter in TestNG
To sort the execution flow of test cases, we can use the 'priority' parameter. You can assign priority starting ‘@Test methods you have. Let’s understand this with an example:
File: Priority.java
package TestNGDemo;
import org.testng.annotations.Test;
public class Priority {
@Test(priority = 3)
public void One() {
System.out.println("Test Case One");
}
@Test(priority = 2)
public void Two() {
System.out.println("Test Case Two");
}
@Test(priority = 1)
public void Three() {
System.out.println("Test Case Three");
}
@Test(priority = 0)
public void Four() {
System.out.println("Test Case Four");
}
}If you execute the above code, Four() would be run first and One() will be the last as it has the least priority – ‘3‘.
Note:
- If multiple
@Testmethods have the same priority, order falls back to alphabetical sequence. - If
priorityparameter is not specified, by default test cases are assigned ‘0 ‘.
preserve-order Attribute in TestNG
Now let’s look at a use case where you might actually want to use alphabetical ordering when executing test cases using testng.xml file.
File: A1.java
package TestNGDemo;
import org.testng.annotations.Test;
public class A1 {
@Test
public void Test1() {
System.out.println("Executing Test1");
}
}
class A2 {
@Test
public void Test2() {
System.out.println("Executing Test2");
}
}
class A3{
@Test
public void Test3() {
System.out.println("Executing Test3");
}
}
class A4{
@Test
public void Test3() {
System.out.println("Executing Test4");
}
}File: testng.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="PreserveOrderDemo"> <test name="test-case1"> <classes> <class name="TestNGDemo.A4" /> <class name="TestNGDemo.A1" /> <class name="TestNGDemo.A3" /> <class name="TestNGDemo.A2" /> </classes> </test> </suite>
As per the default behavior, when you run this suite file, TestNG will run tests in the order they are found. So the output would look like:

Now if you want the classes and methods listed in this file to be run in sorted (alphabetically) order, you just have to set the preserve-order attribute to false in the XML file.
With the preserve-order set false, the output would now appear as:
