4

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

Your clarification helps !!! Thank you !!!

7

Here you have to consider 2 points :

  1. If you are not using any priority in your test method then TestNG assign by default priority=0 to the Test Method
  2. 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

0

If you don't set priority by default, the method will have a priority of zero. When you have multiple methods with the same priority they will be run in alphabetical order.

  • 1
    Please edit your answer to explain what it adds to the existing, accepted answer. – Kate Paulk Sep 13 at 12:33
0

Well Better Way to execute TestNG Class With XML Suite This will Run Your TestNG class methods As per Mentioned In XML

<test name = "Test1">  <!-- Give Specific Package and className -->
    <classes>
        <class name = "Actual_TestNG_Class_Name">  <!-- Give Specific Package and className -->
            <methods>
                <include name = "MethodName1"/> 
                <include name = "MethodName2"/> 
                <include name = "MethodName3"/> 
            </methods>

         </class>
    </classes>      

</test>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.