What is the order of test execution with priority in TestNG?

TestNGRest AssuredDynamic Programming

A TestNG class can have different tests like test1, test2, test3, etc. Once a user runs the TestNG class consisting of various tests, it runs the test cases in an alphabetic order based on the names provided. However, the user can assign the priority to these tests so that these tests can run as per user's priority. Priority starts from "0" which takes the highest priority and as the number moves up, the priority decreases.

In this article, let's analyse how the order of execution with priority works in TestNG.

Scenario 1

If test2 (priority=0), test1 (priority=1), test3 (priority=2), then test2 will run first and after that test1 and so on based on priority.

Approach/Algorithm to solve this problem −

  • Step 1 − import org.testng.annotations.Test for TestNG.

  • Step 2 − Write an annotation as @test

  • Step 3 − Create a method for the @test annotation as test1 with priority=1.

  • Step 4 − Repeat the steps for test2 and test3 with priority "0" and "2" respectively.

  • Step 5 − Now create the testNG.xml.

  • Step 6 − Run the testNG.xml or run the testNG class directly in IDE or compile and run it using command line.

Example

The following code creates a TestNG class and displays the priority order of execution:

import org.testng.annotations.Test;
public class OrderofTestExecutionInTestNG {
   @Test(priority=1)
   public void test1() {
      System.out.println("Starting execution of TEST1");
   }
   @Test(priority=0)
   public void test2() {
      System.out.println("Starting execution of TEST2");
   }
   @Test(priority=2)
   public void test3() {
      System.out.println("Starting execution of TEST3");
   }

}

Output

Starting execution of TEST2
Starting execution of TEST1
Starting execution of TEST3

Scenario 2

If test2 (priority=0), test1(priority=1) and test3 have no priority, then test2 will run first and after that test3 and finally test1. Since test3 doesn't have user defined priority, TestNG assigns its priority=0 and in alphabetical order, test2 comes first and then test3.

Approach/Algorithm to solve this problem −

  • Step 1 − import org.testng.annotations.Test for TestNG.

  • Step 2 − Write an annotation as @test

  • Step 3 − Create a method for the @test annotation as test1 with priority=1.

  • Step 4 − Repeat the steps for test2 and test 3 for priority 0 and don't provide any priority.

  • Step 5 − Now create the testNG.xml.

  • Step 6 − Run the testNG.xml or run the testNG class directly in IDE or compile and run it using command line.

Example

Use the following code to create a TestNG class and display the priority order of execution −

import org.testng.annotations.Test;
public class OrderofTestExecutionInTestNG {
   @Test(priority=1)
   public void test1() {
      System.out.println("Starting execution of TEST1");
   }
   @Test(priority=0)
   public void test2() {
      System.out.println("Starting execution of TEST2");
   }
   @Test()
   public void test3() {
      System.out.println("Starting execution of TEST3");
   }

}

Output

Starting execution of TEST2
Starting execution of TEST3
Starting execution of TEST1
raja
Updated on 09-Mar-2022 10:27:38

Advertisements