Open In App

How to set priority to the test cases in TestNG?

Last Updated : 07 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Prioritization is the management of the order or sequence of tests to be conducted, with an emphasis on the key or basic tests that are conducted first.

There is a way to control the order of test execution while using the '@Test' annotation – it is the priority attribute.

Java
@Test(priority = 1)
public void testMethod() {
    // Test logic here
}

Here, 'priority = 1' means that this test should be run first among all the created tests. That is why the lower the priority number, the higher the chance for the test to run first. TestNG runs the tests from the lower level of priority to the higher level of priority.

Example:

Now that we know how to set priority in TestNG, let us consider an example of how it can be done.

PriorityTest.java

Java
import org.testng.annotations.Test;

public class PriorityTest {

    @Test(priority = 1)
    public void loginTest() {
        System.out.println("Executing Login Test");
        // Login test logic
    }

    @Test(priority = 2)
    public void dashboardTest() {
        System.out.println("Executing Dashboard Test");
        // Dashboard test logic
    }

    @Test(priority = 3)
    public void logoutTest() {
        System.out.println("Executing Logout Test");
        // Logout test logic
    }
}

loginTest is the most important ('priority Equals=1'), thus it will run first.

  • dashboardTest this script will be the second to run ('priority = 2').
  • logoutTest has the lowest priority ('priority = 3') of all the given tests and, therefore, will be executed after all the others.
TestNG-priority-output
TestNG priority output

The right approach towards test case prioritization can enhance the pace of testing and come across with major problems much earlier and hence enhances the effectiveness of the testing process.


Grouping and Prioritizing Tests in TestNG
Visit Course explore course icon
Article Tags :

Similar Reads