skip to content

In TestNG, how does the `priority` attribute of `@Test` decide execution order, including for methods with no priority and for ties?

level: juniorimportance: should knowfreq 50%

answer

  1. a number on each method
  2. lower is scheduled sooner
  3. what the default value is
  4. ties and name order

basics

~10 s

TestNG schedules lower priority values first; the default is 0 and negative values are allowed. Ties fall back to name order, not source order, and dependencies and parallel runs can override the sequence.

solid answer

~40 s

`@Test(priority = n)` is an `int` scheduling hint: lower numbers are scheduled first, and every method without the attribute has the default `0`, so it runs *before* anything marked `1` and after anything negative. When two methods share a priority, TestNG breaks the tie by method name, not by the order they appear in the source file. Priority is compared across all the methods scheduled in one `<test>` element of the suite, so methods of different classes can interleave. It is not a hard guarantee: `dependsOnMethods` and `dependsOnGroups` outrank it, and under a parallel suite run concurrent threads can start methods out of priority order.

code

java · 10 lines
java
import org.testng.annotations.Test;

public class OrderApiSmokeTest {
  @Test(priority = -1) public void warmUpCache() {}
  @Test                public void healthCheck() {}   // priority 0
  @Test(priority = 1)  public void createOrder() {}
  @Test(priority = 1)  public void addLineItem() {}
  @Test(priority = 2)  public void cancelOrder() {}
}
// Sequential order: warmUpCache, healthCheck, addLineItem, createOrder, cancelOrder

go deeper

for a junior

Recall the three facts: lower numbers run first, the default is zero, and negatives are allowed. Say plainly that a method without a priority is not last.

for a middle

Explain ties falling back to name order and why source order is never used. Mention that priority is compared across all methods in one test element, so classes can interleave.

for a senior

Show that you know priority is a hint: dependencies outrank it, parallel threads ignore it in practice, and it creates no skip on failure, so ordered state-passing built on it fails noisily.

for a principal

Frame priority as a smell when used for state handoff. A lead keeps it for warm-up and fail-fast environment checks and pushes ordering needs toward independent tests or explicit dependencies.

## What `priority` is TestNG lets every `@Test` method carry a `priority` attribute, an `int` declared in `org.testng.annotations.Test` with the Javadoc line *"The scheduling priority. Lower priorities will be scheduled first."* It exists because TestNG, unlike a framework that treats every test as fully independent, has always offered ways to influence order. Priority is the lightest of them: it reorders methods without creating any pass/fail relationship between them. Key properties: - **Lower runs first.** `priority = -5` is scheduled before `priority = 0`, which is scheduled before `priority = 3`. - **The default is `0`.** A method that does not mention priority is not "last" or "unordered"; it sits at `0`. - **Negative values are legal.** They are the usual way to push a warm-up or smoke check ahead of everything that kept the default. - **No dependency is created.** If a priority-1 method fails, the priority-2 method still runs and can pass. ## The default-zero trap The most common surprise comes from annotating only some methods. A team writes `priority = 1`, `2` and `3` on three checkout steps and leaves a health check unannotated. The health check has priority `0`, so it runs *first*, not last. If the team wanted it last it needed a priority above 3, and if they wanted the steps to lead they needed negative numbers or a priority on every method. ## Ties: name order, not source order Java reflection does not promise to return methods in source order, so TestNG does not use it to break ties. When priorities are equal, the comparator TestNG uses falls back to names: within a class, `addLineItem` (priority 1) runs before `createOrder` (priority 1) because "a" sorts before "c". Engineers who expect declaration order are surprised when a method renamed during a refactor suddenly moves. | Situation | What decides the order | |---|---| | Different priorities | The lower number first | | Same priority, same class | Method name, alphabetically | | Same priority, different classes | Method name again; the class name is not compared | | No priority anywhere | TestNG's default ordering, which you should not depend on | ## Scope: one `<test>` element, not one class Priority is compared across every method TestNG schedules inside one `<test>` element of the suite file (`testng.xml`). If class `A` has methods with priorities 1 and 2 and class `B` has methods with priorities 1 and 2, a sequential run can execute `A`'s priority-1 method, `B`'s priority-1 method, then both priority-2 methods. Classes interleave. The 6.10 changelog lists priority as more important than `preserveOrder`, which is why the class-by-class order a suite file implies can be broken up by priorities. ## What overrides priority 1. **Dependencies.** The same changelog entry ranks `dependsOnGroups` and `dependsOnMethods` above priority. A method that depends on another waits for it whatever the numbers say. (Dependencies themselves are a separate subject.) 2. **Parallel execution.** When the suite runs methods in parallel, several threads pull work at once. The helper in TestNG's own priority tests asserts only *which* methods ran when the run is parallel, with a comment that the order of methods is then non-deterministic. Priority shapes the queue; it does not serialise threads. 3. **Method interceptors** can reorder the list before scheduling, and listener-based reordering is another subject. ## Example ```java import org.testng.annotations.Test; public class OrderApiSmokeTest { @Test(priority = -1) public void warmUpCache() {} @Test public void healthCheck() {} // priority 0 @Test(priority = 1) public void createOrder() {} @Test(priority = 1) public void addLineItem() {} @Test(priority = 2) public void cancelOrder() {} } // Sequential order: warmUpCache, healthCheck, addLineItem, createOrder, cancelOrder ``` ## When to use it - Use it to put a cheap environment check or warm-up ahead of a long suite, so a broken environment shows up in the first minute. - Avoid using it to make one test feed state to the next. Priority gives no skip on failure, so a broken step produces a wave of misleading failures further down; that is a job for an explicit dependency or, better, for independent tests. - Remember the contrast with JUnit Jupiter, which has no `priority` attribute on its `@Test`; ordering there is configured differently. The interview signal is precise knowledge of the three defaults people get wrong: lower first, default zero, and name-ordered ties, plus the honesty to say priority is a scheduling hint rather than a guarantee.

  • Why does a TestNG method with no priority run before one marked priority = 1?
    Because an absent `priority` is not "unset", it is the default value `0`, and TestNG schedules lower values first. To make unannotated methods run last you must give the others negative priorities or give the late method a value above every other one.
  • Does TestNG priority still hold when the suite runs methods in parallel?
    Not as a guarantee. Priority orders the queue, but several threads take work from it at once, so a priority-2 method can start while a priority-1 method is still running, or even before it. The helper in TestNG's own priority tests checks only the set of methods in parallel mode, noting that the order is non-deterministic.
  • If priority and dependsOnMethods disagree in TestNG, which wins?
    The dependency. TestNG's changelog ranks dependsOnGroups and dependsOnMethods above priority, so a method waits for the methods it depends on whatever its number says. Priority only orders methods that are otherwise free to run.

  • Higher priority numbers run first, like an urgent ticket.
  • Methods without a priority run after all the prioritised ones.
  • Methods with equal priority run in source-declaration order.
  • Priority only reorders methods inside a single class.
  • Priority guarantees order even when methods run in parallel.
  • A failing lower-priority method makes later methods skip.