In my professional and personal experience, I have interacted with many people (be it on networking sites or in interviews) on working of the TestNG Priority.

The most common correct answer I have heard is that it executes tests based on alphabetical order and the most common incorrect answer is that it will be executed in random order.

I am writing this post in order to clear all the misconceptions around priorities and its execution order.

Let us begin with a basic and a straight forward example.

The output of the above code will be:

Methods get executed in Alphabetical order.

Randomly placed methods:

In this example, we still have the same methods as the previous example but just placed in random order to test the output.

The output of the above code will be:

As methods are executed in Alphabetical order we still get the same result as the previous example. The method position does not change the execution order of the TestNG.

Note: Methods are executed in Alphabetical order but when we have some method name starting with capital letters like Random() and some with a small letter random() then method with a capital letter will be executed before a method will small letter.

Adding priority to a single @Test:

Let us add priority to method A()and check if there is any difference in the output order.

After looking at this code, I get a few questions, and here are some of the question that I can recollect:
Q. Why are we not assigning priorities to other tests?
Q. Will it work if I assign priority only to a single test?
Q. Can I assign some priorities to other tests before giving my answer?

After answering all of the above questions, 95% of the time I still get the answer as:

Execution order will still be the same as the last code as we have assigned priority=1 to method A() and TestNG executes tests in Alphabetical order.

Let us first look at the output and then understand why it got executed in such an order.

In this case, the method A() is executed at the end because the default priority of TestNG is 0.

We have not explicitly provided any priority to method B(), C(), and D(). So they all are treated with default priority. Hence, they were executed before the method A().

If we open interface from TestNG.jar then we’ll be able to see below method implementation with the default value.

Please note, The above execution order is valid when we are assigning only priority to @Test annotation. Execution order will change if we add dependsOnMethods() to @Test annotation. Refer below block of code from interface of TestNG.

P.S. I have created the methods starting with Captial letters only to demonstrate the execution order. As per Java naming convention, the method name should always follow the camel case.