As we know testNG test methods are executed in alphabetical order by default, but testNG provides priority keyword to prioritize test case execution order and the value starts from -n to n…
Priority value starts from any number from -ve to +ve, 0 is default (if nothing is set)
For a method less priority number, executes first
| TestCase | Priority |
| tc1 | |
| tc2 | -1 |
| tc3 | -2 |
| tc4 | 2 |
| tc5 | 1 |
Order of execution –
tc3 – tc2 – tc1 – tc5 – tc4
Let’s see the code implementation:
import org.testng.annotations.Test;
public class testNGPr {
@Test(priority=-12)
public void test1()
{
System.out.println("test1: priority -12");
}
@Test(priority=-2)
public void test2()
{
System.out.println("test2: priority -2");
}
@Test(priority=3)
public void test3()
{
System.out.println("test3: priority 3");
}
@Test(priority=2)
public void test4()
{
System.out.println("test4: priority 2");
}
@Test(priority=0)
public void test5()
{
System.out.println("test5: priority 0");
}
@Test
public void test6()
{
System.out.println("test6: test231 no priority");
}
@Test
public void test7()
{
System.out.println("test7: test2311 no priority");
}
}
OutPut –
test1: priority -12
test2: priority -2
test5: priority 0
test6: test231 no priority
test7: test2311 no priority
test4: priority 2
test3: priority 3
As you can see above result, the execution order is as follows –
negative priority – with ‘-1’ being lowest priority (-1 executes last in all -ve priority list)
priority with 0 or no priority set (executes in alphanumerical order for priority 0 or no priority)
positive priority – with 1 being highest proirity (1 executes first in all +ve priority list)





1 Comment