TestNG Priority | How To Set Test Case Priority In TestNG
Sometimes, in automation, we need to arrange the test methods in a specific sequence or prioritize some methods over others.
TestNG let us include a priority attribute in the @Test annotation that helps us manage situations like this. We can arrange the test methods in the desired order by assigning a value to this priority attribute.
If test priority is not defined while, running multiple test cases, TestNG assigns all @Test a priority as zero(0).
TestNG Code without Priority (Alphabetical Order)
If we do not specify a priority, TestNG will execute the @Test methods in alphabetical order based on their method names, regardless of where they are implemented in the code.
package softwareTestingMaterial;
import org.testng.annotations.Test;
public class TestNGPriorityAnnotations {
@Test
public void b_method(){
System.out.println("Its me method B");
}
@Test
public void c_method(){
System.out.println("Its me method C");
}
@Test
public void d_method(){
System.out.println("Its me method D");
}
@Test
public void a_method(){
System.out.println("Its me method A");
}
@Test
public void e_method(){
System.out.println("Its me method E");
}
}
Output
Even though we arranged the methods randomly (b, c, d, a, e), TestNG executed the methods in alphabetical order based on their method names.
Its me method A
Its me method B
Its me method C
Its me method D
Its me method E
How to set Priority in TestNG
In the previous example, we have shown you test code without setting Priority. Now we will let TestNG run the script against the priority assigned by us.
Note: Test Case with lower priority will be executed first.
package softwareTestingMaterial;
import org.testng.annotations.Test;
public class TestNGPriorityAnnotations {
@Test(priority=4)
public void b_method(){
System.out.println("Its me method B");
}
@Test(priority=0)
public void c_method(){
System.out.println("Its me method C");
}
@Test(priority=3)
public void d_method(){
System.out.println("Its me method D");
}
@Test(priority=1)
public void a_method(){
System.out.println("Its me method A");
}
@Test(priority=6)
public void e_method(){
System.out.println("Its me method E");
}
}
Output
We have assigned priorities of 0, 1, 3, 4, and 6 to the methods. This means that the method with priority 0 will be executed first, followed by the method with priority 1, and so on. The order of the method names alphabetically will not be taken into consideration because we have assigned priorities.
Its me method C
Its me method A
Its me method D
Its me method B
Its me method E
Methods with Same Priority in TestNG
In the previous example, we have shown you test code without setting Priority. Now we will let TestNG run the script against the priority assigned by us.
Note: Test Case with lower priority will be executed first.
package softwareTestingMaterial;
import org.testng.annotations.Test;
public class TestNGPriorityAnnotations {
@Test(priority=4)
public void b_method(){
System.out.println("Its me method B");
}
@Test(priority=0)
public void c_method(){
System.out.println("Its me method C");
}
@Test(priority=3)
public void d_method(){
System.out.println("Its me method D");
}
@Test(priority=4)
public void a_method(){
System.out.println("Its me method A");
}
@Test(priority=6)
public void e_method(){
System.out.println("Its me method E");
}
}
Output
We have assigned priorities of 0, 3, 4, 4, and 6 to the methods. This means that the method with priority 0 will be executed first, followed by the method with priority 3, and so on. Whereas for priority 4, TestNG will execute based on the alphabetic order.
Its me method C
Its me method D
Its me method A
Its me method B
Its me method E
Combining both prioritized(having the same priority) and non-prioritized methods:
package softwareTestingMaterial;
import org.testng.annotations.Test;
public class TestNGPriorityAnnotations {
@Test()
public void c_method(){
System.out.println("Its me method C");
}
@Test()
public void b_method(){
System.out.println("Its me method B");
}
@Test(priority=6)
public void a_method(){
System.out.println("Its me method A");
}
@Test(priority=0)
public void e_method(){
System.out.println("Its me method E");
}
@Test(priority=6)
public void d_method(){
System.out.println("Its me method D");
}
}
Output
TestNG will execute non-prioritized methods ‘c’ and ‘b’ first based on the alphabetical order and then it will execute methods ‘a’, ‘e’ and ‘d’. Here it executes ‘e’ first as it was having highest priority(0). Here the priority of methods ‘a’ and ‘d’ are same, TestNG considered the alphabetical order of their methods names. So ‘a’ was executed first and then ‘d’.
Its me method B Its me method C Its me method E Its me method A Its me method D
To run a set of test cases in a specific sequence, you can use the “Priority” feature in TestNG
You could find the complete TestNG tutorial here.