</>

Technology

TestNG

Difficulty

Beginner

Interview Question

What is the default priority of test cases in TestNG?

The default priority in TestNG is 0. Tests with lower priority numbers run first, so a test with priority 0 runs before a test with priority 1.

Answer

Default Priority of Test Cases in TestNG

The default priority assigned to a @Test method when no priority is specified is integer value 0.

Key rule: Lower priority number = runs first
Java
@Test(priority = 1)
public void testWithPriority1() {
    System.out.println("Runs SECOND");
}

@Test
// No priority = defaults to 0
public void testWithDefaultPriority() {
    System.out.println("Runs FIRST (priority 0)");
}
Order of execution: testWithDefaultPriority()testWithPriority1()

Extended example:

Java
@Test(priority = -1)
public void negativeTest() {
    System.out.println("1st — priority -1");
}

@Test
// Default priority = 0
public void defaultTest() {
    System.out.println("2nd — priority 0 (default)");
}

@Test(priority = 1)
public void firstPriority() {
    System.out.println("3rd — priority 1");
}

@Test(priority = 2)
public void secondPriority() {
    System.out.println("4th — priority 2");
}

What happens with same priority? If two tests have the same priority (including the default 0), TestNG sorts them alphabetically by method name:

Java
@Test  // priority 0
public void loginTest() { }  // runs before searchTest (l < s alphabetically)

@Test  // priority 0
public void searchTest() { }

Summary table:

ScenarioExecution order
No priority (default 0)Before priority 1, 2, 3...
priority = -1Before default (0)
priority = 0 (same as default)Same bucket, sorted alphabetically
priority = 1After default (0)

Best practice: Prefer dependsOnMethods for explicit ordering rather than relying on priority numbers alone.

Follow AutomateQA

Related Topics