How to Control the order of execution in TestNG?
Many of you would be wondering if we can control the order of execution in TestNG. Say, if you have 4 Test Cases in a single java file. Based on alphabetical order TestNG will execute the Test Cases. If you want to execute in a specific order as per your requirement, then we can do by prioritizing the order of execution.
Here we are taking an example : TestNGOrder.java which contains 4 Test Cases in a java file. If we execute the Test Cases, TestNG will execute without following the order which we gave the Test Cases. Hence we can set our priority for the order of execution in TestNG like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import org.testng.annotations.Test; public class preserveOrderTestNG { @Test(priority=0) public void method1() { System.out.println("method1() has executed "); } @Test(priority=3) public void meth2() { System.out.println("method2() has executed "); } @Test(priority=2) public void meth3() { System.out.println("method3() has executed "); } @Test(priority=1) public void meth4() { System.out.println("method4() has executed "); } } |
OUTPUT :