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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class PriorityTest { @Test public void A() { System.out.println("This is A"); } @Test public void B() { System.out.println("This is B"); } @Test public void C() { System.out.println("This is C"); } @Test public void D() { System.out.println("This is D"); } } |
The output of the above code will be:
1 2 3 4 |
This is A This is B This is C This is D |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class PriorityTest { @Test public void C() { System.out.println("This is C"); } @Test public void B() { System.out.println("This is B"); } @Test public void D() { System.out.println("This is D"); } @Test public void A() { System.out.println("This is A"); } } |
The output of the above code will be:
1 2 3 4 |
This is A This is B This is C This is D |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class PriorityTest { @Test public void C() { System.out.println("This is C"); } @Test public void B() { System.out.println("This is B"); } @Test public void D() { System.out.println("This is D"); } @Test (priority = 1) public void A() { System.out.println("This is A"); } } |
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.
1 2 3 4 |
This is B This is C This is D This is A |
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 Test.java
interface from TestNG.jar
then we’ll be able to see below method implementation with the default value.
1 2 3 4 |
/** * The scheduling priority. Lower priorities will be scheduled first. */ int priority() default 0; |
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 Test.java
interface of TestNG.
1 2 3 4 5 6 7 8 9 10 11 12 |
/** * The list of methods this method depends on. There is no guarantee * on the order on which the methods depended upon will be run, but you * are guaranteed that all these methods will be run before the test method * that contains this annotation is run. Furthermore, if any of these * methods was not a SUCCESS, this test method will not be * run and will be flagged as a SKIP. * * If some of these methods have been overloaded, all the overloaded * versions will be run. */ public String[] dependsOnMethods() default {}; |
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.
Leave a Reply