Prioritizing Tests in TestNg
Prioritizing tests in testng will discuss about the order of execution tests in test suite. We will write test methods our own way using @Test annotation. After writing the test cases we will execute the test cases either normally or from the testng.xml file. After execution of the tests if we observe the order of execution then we can find that tests executed by taking the alphabetical order. And all the tests will have the equal priority as we did not set any priority to the tests.
In order to set the priority to the tests then we can use one of the @Test attributes called “Priority”. By default all the tests will have the same priority called Zero(i.e. If you not set any priority then it will take the priority as Zero).
If you give the priority equal to all the test cases then all the tests will execute in alphabetical order. Here we will see how the priority will work while executing the tests by taking few examples:
1. In the below example we will see test case execution flow without setting any priority to the tests (i.e. all the tests will have the priority as ZERO).
public class WithOutPriority { @Test public void secondTest() { System.out.println("In secondTest method"); } @Test public void thirdTest() { System.out.println("In thirdTest method"); } @Test public void fourthTest() { System.out.println("In fourthTest method"); } }
Below is the Output for the same:
If we observe the above output of all the 3 methods they executed in the alphabetical order. We have the order in test class is secondTest, thirdTest and fourthTest. But after execution the output is fourthTest, secondTest and thirdTest.
2.In the below example we will see test case execution flow with Priority attribute. Will take three test methods and will set the priority to them like one is without priority attribute and remaining
with 1 and 2.
public class PartialPriority { @Test(priority = 1) public void secondTest() { System.out.println("In secondTest method"); } @Test public void thirdTest() { System.out.println("In thirdTest method"); } @Test(priority = 2) public void fourthTest() { System.out.println("In fourthTest method"); } }
Below is the Output for the same:
If we observe the above output of all the 3 methods they executed as per the priorities we set. The test without priority executed first as the default priority is equal to ZERO and it takes the high priority. And then priority = 1 will execute and last priority = 2 will execute.
3.In the below example we will see test case execution flow with Priority attribute. Will take three test methods and will set the priority to them like 1,2 and 3
public class PriorityTest { @Test(priority = 1) public void secondTest() { System.out.println("In secondTest method"); } @Test(priority = 3) public void thirdTest() { System.out.println("In thirdTest method"); } @Test(priority = 2) public void fourthTest() { System.out.println("In fourthTest method"); } }
Below is the Output for the same:
If we observe the above output of all the 3 methods they executed as per the priorities we set. The test with priority = 1 will execute first and priority = 2 will execute second and priority = 3 will execute last.
This way we can prioritize the tests in testNG to control the execution flow.
Please watch You tube video for better understanding.