If I use the TestNg priority option, what would be the order of execution for tests?

 I have three methods as mentioned below, two with priority ( 0 , 1 ) and third method(Test) with no priority attribute. What is the order of execution? Code snippet given below..


@Test(priority = 1)
    private void test1() {
        System.out.println("Test Priority One- Revision");
    }
    @Test(priority = 0)
    private void test2() {
        System.out.println("Test Priority Zero - Revision");
    }
    @Test
    private void ztestNP() {
        System.out.println("Test NoPriority- Revision");
    }

What is the expected Order ? I expected

Test Priority Zero - Revision Test 

Priority One- Revision Test 

NoPriority- Revision

But Getting


Test Priority Zero - Revision

Test NoPriority- Revision 

Test Priority One- Revision


Answered by Anil Jha

Here you have to consider 2 points : If you are not using any TestNG priority in your test method then TestNG assign by default priority=0 to the Test Method If there is same priority assign to test methods then execution order will be alphabetically. So in your case it is adding priority=0 internally to your ztestNP() methods

@Test(priority=0)
public void ztestNP() {
    System.out.println("Test NoPriority- Revision");
}
So your output what you are getting is correct.
Now if you want to test execution order of same priority methods(will execute in alphabetical order)
e.g.
@Test(priority = 1)
public void btest1() {
    System.out.println("Test Priority One- Revision");
}
@Test(priority = 1)
public void atest2() {
    System.out.println("Test Priority Zero - Revision");
}
@Test(priority = 0)
public void ztestNP() {
    System.out.println("Test NoPriority- Revision");
}

Then output will be :

Test NoPriority- Revision

Test Priority Zero - Revision

Test Priority One- Revision



Your Answer

Interviews

Parent Categories