Table of contents
1.
Introduction
2.
Example
3.
Test Case with Same Priority
4.
Frequently Asked Questions
5.
Conclusions
Last Updated: Mar 27, 2024
Easy

Test Priority in TestNG

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The priority function is used to set the priorities for different test cases in Selenium. We can assign only integer values as a priority, and the value can be either positive, negative, or zero. In the priority, function lower the value of the priority number higher is the priority of the test case. The default priority for all the test cases is always zero. The developer can assign only one priority value to a test case. The syntax for the priority function looks like this,

@Test (priority = 1)
public void Test1(){
   //Code to be executed
}

In the above example, we assign the priority of Test1 as 1.

Example

Let us look at an example of the priority function to understand it better.

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import org.openqa.selenium.chrome.ChromeDriver;


public class priorityTest {
   WebDriver driver = new ChromeDriver();


   @Test (priority = 1)
   public void FirstTestCase() {
      driver.close();
      System.out.println("This is the first test case");
   }


   @Test (priority = 0)
   public void SecondTestCase() {
      System.out.println("This is the second test case"); 
   }
}

In the above example, we create a class named priorityTest. We assign the priority of FirstTestCase as one and the priority of SecondTestCase as zero. Since zero is the smaller number, SecondTestCase will get executed before FirstTestCase. In case no priority was declared, the test cases would get executed in alphabetical order, i.e., FirstTestCase would be executed before SecondTestCase.

Test Case with Same Priority

Let us look at an example, where two or more test cases are provided the same priority and observe the flow of execution,

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import org.openqa.selenium.chrome.ChromeDriver;


public class priorityTest {
   WebDriver driver = new ChromeDriver();


   @Test (priority = 0)
   public void FirstTestCase() {
      driver.close();
      System.out.println("This is the first test case");
   }


   @Test (priority = -1)
   public void SecondTestCase() {
      System.out.println("This is the second test case");           
   }


   @Test (priority = 0)
   public void ThirdTestCase(){
      System.out.println("This is the third test case");
   }
}

In the above example, we have created three test cases: FirstTestCase, SecondTestCase, and ThirdTestCase. We assigned the priority for FirstTestCase as zero, for SecondTestCase as -1, and ThirdTestCase as zero. Since SecondTestCase has the smallest priority value, SecondTestCase will be executed first. 

Now the question arises, what happens with FirstTestCase and ThirdTestCase. It's simple, and they will get executed in alphabetical order. FirstTestCase will get executed before ThirdTestCase. So the final sequence of execution for the test cases will look like this,

SecondTestCase -> FirstTestCase -> ThirdTestCase

Check this out : Xpath in Selenium

Frequently Asked Questions

  1. What is Automation Testing?
    Automation Testing can be understood as the use of specialized or specifically designed tools to automate the execution of designed test cases without any human involvement.
  2. What tasks can be performed using Automation Testing tools?
    Automation testing tools can perform all the tasks, such as accessing the test data, controlling the execution of tests, and comparing the observed results against the expected result with more ease and efficiency. We can also use the Automation Testing tools to generate test reports.
  3. What is Selenium?
    Selenium is one of the most famous open-source tools used for Functional Automation Testing. It can be used to write test cases in several popular programming languages like JavaScript, Python, Perl, Java, etc.
  4. What is the default priority of a test case in Selenium?
    The test cases in Selenium are automatically assigned a priority of zero in case no priority is allotted to the test case.
  5. What is test priority in the TestNG framework?
    The priority function is used to set the priorities for different test cases in Selenium. We can assign only integer values as a priority, and the value can be either positive, negative, or zero. In the priority, function lower the value of the priority number higher is the priority of the test case.

Conclusions

This Blog covered all the necessary points regarding the test priority in the TestNG framework. We further looked at an example of test priority and the scenario where two or more test cases are allotted the same priority.

Don’t stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Live masterclass