Few days ago I was taking a look over a class which contained many @Test methods (from org.testng.annotations of course ) and whenever it was running, I was getting a different order of the tests in the results’ summary.
So this got me thinking and a quick solution came to mind: test dependency (dependsOnMethods / dependsOnGroups) but this would then imply that if the initial test fails, the execution would stop right then and there. I then found out about test method priority and it seems to be exactly what I was looking for and very easy to use.
Given the following example let’s see how we can obtain a desired order of execution: A > B > C
In the above case, we get this order of execution: B > C > A which isn’t exactly what we wanted, let’s see how we can change that.
A few things need to mentioned about test method priorities:
- lowest priority (can also be negative) will be executed first, thus -5 will run before priority 1
- when no priority is specified, the default priority is 0, in our example, testB has the default priority
Since we’re aiming to obtain the A -> B ->C flow, we should use, the lowest priority for testA and set B and C to 1 and 2 respectively: