top of page

TestNG Tutorial

Test Priorities In TestNG

What Is Test Priority In TestNG ?

Priority is an attribute that enables the user to define the order of the test execution. If we have multiple test cases and we want to run them in a particular order, we can use 'priority' attribute to set test priority. The default priority of a test is 'zero'.​
Syntax to set test priority :

@Test(priority=1) public void testCase2() { }


Priority can be set in following ways :

  • Positive Test Priority : Run in the sequence of highest priority.

package testNGexamples; import org.testng.annotations.Test; public class PriorityTest { @Test(priority=3) public void testCase1() { System.out.println("For testCase1 : Test Priority is set 3"); } @Test(priority=1) public void testCase2() { System.out.println("For testCase2 : Test Priority is set 1"); } @Test(priority=2) public void testCase3() { System.out.println("For testCase3 : Test Priority is set 2"); } }


Output :

[RemoteTestNG] detected TestNG version 7.4.0 For testCase2 : Test Priority is set 1 For testCase3 : Test Priority is set 2 For testCase1 : Test Priority is set 3 PASSED: testCase3 PASSED: testCase2 PASSED: testCase1 =============================================== Default test Tests run: 3, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 3, Passes: 3, Failures: 0, Skips: 0 ===============================================​




  • Negative Test Priority : Run before the default test priorities.

package testNGexamples; import org.testng.annotations.Test; public class NegativeTestPriority { @Test public void testCase1() { System.out.println("For testCase1 : Test Priority is Default."); } @Test(priority=-1) public void testCase2() { System.out.println("For testCase2 : Test Priority is set -1 and It will Execute Before the Default Priority Test."); } }



Output :

[RemoteTestNG] detected TestNG version 7.4.0 For testCase2 : Test Priority is set -1 and It will Execute Before the Default Priority Test. For testCase1 : Test Priority is Default. PASSED: testCase2 PASSED: testCase1 =============================================== Default test Tests run: 2, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================



  • Same Test Priority : If tests have same priority then it will run in alphabetical order.

package testNGexamples; import org.testng.annotations.Test; public class SameTestPriority { @Test(priority=2) public void bMethod() { System.out.println("For bMethod : Test Priority is 2"); } @Test(priority=2) public void aMethod() { System.out.println("For aMethod : Test Priority is 2"); } @Test(priority=1) public void cMethod() { System.out.println("For cMethod : Test Priority is 1"); } @Test(priority=3) public void dMethod() { System.out.println("For dMethod : Test Priority is 3"); } }



Output :

[RemoteTestNG] detected TestNG version 7.4.0 For cMethod : Test Priority is 1 For aMethod : Test Priority is 2 For bMethod : Test Priority is 2 For dMethod : Test Priority is 3 PASSED: dMethod PASSED: bMethod PASSED: cMethod PASSED: aMethod =============================================== Default test Tests run: 4, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 4, Passes: 4, Failures: 0, Skips: 0 =============================================== ​ ​


*If tests have no priorities then it will run in alphabetical order.



Refer next page Parameterization In TestNG
bottom of page