TestNG Tutorials 23: @Test Annotation – Don’t Confuse TestNG With Duplicate Priorities

Hello Folks,

We learn about priority of test methods in a TestNG class in this post.
TestNG Tutorials 22: @Test Annotation – Games Of Priority Of Methods In TestNG

Do you know that TestNG will execute methods in unexpected order if you provide duplicate priorities and can create a nightmare for you. We will see some examples below:
Scenario: 
Create a testng class with some test methods and provide them sequential priority starting from 1. Create another testng class with some test methods and provide them sequential priority starting from 1. Include both classes in testng.xml and run. You would expect first test methods of class 1 will run followed by methods of class 2. Let’s see here:
Class Research1:

Class Research2:

TestNg.xml:

Output:

 

You can notice TestNG didn’t run test methods class by class. It grouped test methods based on similar priority and executed one after one. It may be problem for us if first method (testB1) of second class is dependent on 3rd methods(testA3) of first class but here testB1 executed by testA3. To handle these cases, you must need to pass priorities in sequence.

Solution:

Change priority of methods testB1, testB2, testB3 in second class “Research2” as 4, 5 and 6 respectively and run testng.xml.

Output:

Now TestNG runs methods class after class. Similar types of situation can occur in other scenarios also. So always try to provide priority in sequential order not duplicate.

More about TestNG in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

8 thoughts on “TestNG Tutorials 23: @Test Annotation – Don’t Confuse TestNG With Duplicate Priorities”

  1. Hello Amod,

    I see that the TestNG execute class by class. All methods of Class1 are executed before and then all methods of Class 2 are executed. I have provided duplicate priorities. Can you guide me why this happening and not the other way around. As per the above knowledge TestNG will group all the classes mentioned in the testng.xml file and execute all the methods based on the low priority. But in my case this is not happening.

    Class 1

    package mainPack;

    import org.testng.annotations.Test;

    public class Class1 {

    @Test(priority = 1)
    public void Method1() {

    System.out.println(“Class 1 Method 1”);
    }

    @Test(priority = 2)
    public void Method2() {

    System.out.println(“Class 1 Method 2”);
    }

    @Test(priority = 3)
    public void Method3() {

    System.out.println(“Class 1 Method 3”);
    }

    }

    Class 2

    package mainPack;

    import org.testng.annotations.Test;

    public class Class2 {

    @Test(priority = 1)
    public void Method1() {

    System.out.println(“Class 2 Method 1”);
    }

    @Test(priority = 2)
    public void Method2() {

    System.out.println(“Class 2 Method 2”);
    }

    @Test(priority = 3)
    public void Method3() {

    System.out.println(“Class 2 Method 3”);
    }

    }

    TestNG xml file


    Output

    [RemoteTestNG] detected TestNG version 7.0.0

    Class 1 Method 1
    Class 1 Method 2
    Class 1 Method 3
    Class 2 Method 1
    Class 2 Method 2
    Class 2 Method 3

    ===============================================
    Suite
    Total tests run: 6, Passes: 6, Failures: 0, Skips: 0
    ===============================================

  2. testA1 from Research1 class and value is : 10
    testA2 from Research1 class and value is : 11
    testA3 from Research1 class and value is : 12
    testB! from Research1 class and value is : 10
    testB2 from Research1 class and value is : 11
    testB3 from Research1 class and value is : 12

    How to get this output with having priority in both classes as 1,2,3 ?

  3. Hello,
    I have 2 classes with each class containing 10 test methods prioritized from 1 to 10. Now when i place these in testng.xml only priority 1 tests from each of the classes are executed and then script exits.

    Note: for each test method I am opening browser, performing action and then closing the browser.

    If I remove the priorities or provide the priorities in sequence then it works fine. Please help me with this, it’s bothering me a lot.

  4. Good One Amod… Just a question, if i have 100 class with each having 3 test methods, then should i give my priority as running number starting from 1 for 1st @test method in class 1 to 300 for 3rd @test method in 300 class.. is there any alternative for this to make testng read class wise..

    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *