TestNG @Test Priority in Selenium

TestNG @Test Priority in Selenium

Table of Contents

What is Test Priority?

In TestNG, “Prioritization” means assigning the Execution Priority on Test Case. For example, we want to execute the test cases in order when there are multiple test cases, i.e., first we need to execute the “Registration” test case before login. The priority on the test case will define the Execution order of Test Case. It can be defined at the Method Level. To assign the priority to a test case, you need to define the test priority at the method level within the @Test annotation. Learning these techniques is an essential part of any Selenium Course, where you can gain practical knowledge on test case prioritization and execution flow.

In order to set the priority, we need to add annotation as @Test (priority=?). The default value for priority will be zero. If you don’t mention the priority, it will take as “priority=0” for all the test cases and execute. Lower priorities will be executed first. If we define priority as “priority=” for multiple test cases, then these test cases will get executed only after all the test cases which don’t have any priority and set priority to 0 as the default priority.

Syntax:

//Valid Syntax
@Test(priority = 1)
//Invalid Syntax
@Test(PRIORITY = 1)

TestNG Test Cases without Priority

Let’s consider below TestNG program where we will not define any priority with the test method.

Step 1: Launch the Eclipse

package testNGPriority;

import org.testng.annotations.Test;

public class NoPriority_Example {
@Test 
public void one() 
{ 
  System.out.println("First Test Case"); 
} 
@Test 
public void two()
{ 
  System.out.println("Second Test Case"); 
} 
@Test 
public void three()
{ 
  System.out.println("Third Test Case"); 
} 
}

Step 2: Right click on Java class > Run As and then click on the TestNG Test.

Output:

NoPriority.PNG

In the above program, we declared a class with three test methods: one(), two(), and three(), each without any assigned priority. When running the tests, we observed that TestNG executed these methods in alphabetical order, following the method names rather than the sequence in which they were implemented in the code.

This default behavior ensures a predictable, consistent order for execution, even when no specific priority is defined.

However, to control the execution order according to our specific requirements perhaps starting with essential setup methods or prioritizing critical test cases. we can use the @Test annotation to assign priorities explicitly.

By setting priority values with the @Test annotation, we can customize the order to better suit our testing goals, ensuring that key test methods are executed in the desired sequence. This approach provides greater flexibility and control, especially in complex test scenarios where method dependencies or ordered execution are crucial for comprehensive testing.

How to Set Priority in TestNG for Test Cases?

Let’s consider below TestNG program where we will define priority with the test methods to execute in sequential order.

Step 1: Launch the Eclipse

package testNGPriority;

import org.testng.annotations.Test;

public class testNGPriorityExample {
@Test(priority = 1) 
public void one() 
{ 
  System.out.println("First Test Case"); 
} 
@Test(priority = 2) 
public void two()
{ 
  System.out.println("Second Test Case");
} 
@Test(priority = 3) 
public void three()
{ 
  System.out.println("Third Test Case"); 
} 
}

Step 2: Right click on Java class > Run As and then click on the TestNG Test.

Output:

priorityex.PNG

After assigning the priority to each test method the output has changed. From the above output you can see the test method having lower priority is executed first i.e. execution is happened in sequential order based on priority assigned to the test method.

Thus, alphabetical order method name has not been considered to the test methods as we provide priorities and all the test cases has got executed.

Test Methods with Same Priority

In TestNG, when multiple test methods are assigned the same priority level, TestNG applies a default execution rule to determine their order.

This allows us to explore how TestNG handles test methods with equal priority values and in what sequence they execute. Let’s look at a TestNG program where we define several test methods with the same priority.

By observing this program, we can examine TestNG’s execution behavior and understand how it organizes methods alphabetically when priority levels are identical.

This feature is particularly useful for testers who want a deeper understanding of TestNG’s default ordering mechanism and how it can help achieve consistent, organized test runs even when methods share priority.

Step 1: Launch the Eclipse

package testNGPriority;

import org.testng.annotations.Test;

public class SamePriorityExample {
@Test(priority = 1) 
public void firstTest() 
{ 
  System.out.println("First test method"); 
} 
@Test(priority = 1) 
public void secondTest()
{ 
  System.out.println("Second test method"); 
} 
@Test(priority = 0) 
public void thirdTest()
{ 
  System.out.println("Third test method"); 
} 
@Test(priority = -1) 
public void fourthTest()
{ 
  System.out.println("Fourth test method"); 
} 
@Test(priority = -2) 
public void fifthTest()
{ 
  System.out.println("Fifth test method"); 
} 
}

Step 2: Right click on Java class > Run As and then click on the TestNG Test.

Output:

samepriority.PNG

In the example provided, we have a class containing five test methods, each assigned specific priority values that determine the execution order in TestNG. TestNG follows a priority-based approach, where lower values have higher execution precedence. However, if two or more methods are assigned the same priority, TestNG defaults to executing those methods in alphabetical order by their method names.

For instance, in this case, the methods firstTest() and secondTest() both have a priority value of 1. Since these two methods share the same priority, TestNG checks the alphabetical order of their names to decide the execution sequence.

Here, TestNG detects that firstTest() should run before secondTest() because the letter ‘f’ comes before ‘s’ alphabetically. This ensures consistency in execution when priority conflicts arise, helping maintain a predictable test order.

Combination of Prioritized  and Non-prioritized methods

Let’s consider the following TestNG program where we cover two scenarios within a single TestNG class. First, we have test methods with the same priority value, and second, we include multiple methods without any assigned priority. This setup allows us to observe how TestNG handles tests when there are methods with identical priority levels and methods that lack priority altogether.

In TestNG, when multiple methods share the same priority, they execute in alphabetical order by their method names. However, if there are also non-prioritized methods in the same class, TestNG will execute these methods last, again in alphabetical order. This behavior illustrates how TestNG’s default execution rules prioritize based on priority values first, then organize remaining non-prioritized tests.

By experimenting with these configurations, testers can gain a deeper understanding of how TestNG organizes tests and how to use the @Test annotation with priority settings effectively, especially when custom execution orders are needed.

This understanding becomes invaluable for complex test suites, allowing testers to have more control over test execution flow and better manage dependencies between tests.

Step 1: Launch the Eclipse

Step 2: Create Class name as “TestNGPriorityTest”

package testNGPriority;

import org.testng.annotations.Test;

public class TestNGPriorityTest {
@Test 
public void m1_c() 
{ 
  System.out.println("m1_c"); 
} 
@Test 
public void m1_b()
{ 
  System.out.println("m1_b"); 
} 
@Test(priority = 1) 
public void m1_a()
{ 
  System.out.println("m1_a"); 
} 
@Test(priority = 1) 
public void m1_d()
{ 
  System.out.println("m1_d"); 
} 
@Test(priority = 0) 
public void m1_e()
{ 
  System.out.println("m1_e"); 
} 
@Test(priority = 2) 
public void m1_f()
{ 
  System.out.println("m1_f"); 
  } 

}

Step 2: Right click on Java class > Run As and then click on the TestNG Test.

Output:

priority.PNG

First preference: In the above class, m1_b() and m1_c() which are non-prioritized test methods have been executed based on alphabetical order ‘b’ and then ‘c’.

Second preference: m1_e() which is prioritized methods has been executed first because of having the highest priority (0). As the test methods m1_a() and m1_d() have the same priority. Therefore, TestNG will considered the alphabetical order of their methods names. So, between them, “m1_a” was executed first and then “m1_d”.

Third preference:  m1_f() is executed last by TestNG because of having the lowest priority (2).

Let’s consider the scenario where we automate Google home page and print the title of home page by using the priority feature.

Step 1: Launch the Eclipse

Step 2: Create the Class name as “GoogleTest”

package sampletestpackage;

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

public class GoogleTest {
WebDriver driver;
@Test(priority = 1) 
public void driverSetup()
{ 
System.setProperty("webdriver.chrome.driver", "D:\\Drivers\\geckodriver.exe");
driver=new ChromeDriver(); } 
@Test(priority = 2)
public void getURL()
{ 
  driver.get("https://www.google.com"); 
} 
@Test(priority = 3) 
public void getTitle()
{ 
  String title = driver.getTitle(); 
  System.out.println(title); 
  } 
@Test(priority = 4) 
public void closeBrowser()
{ 
  driver.close(); 
  System.out.println("Test successfully passed"); 
  } 
}

Output:

google.PNG

Conclusion:

In TestNG, setting a specific execution order for test methods is straightforward with the use of priorities. By assigning priority values, you can control the sequence in which tests run, ensuring that essential steps are completed in a defined order.

This approach is particularly valuable for complex test scenarios where certain tests depend on the outcomes of previous ones. With TestNG’s priority feature, creating an organized and efficient testing flow becomes easy, allowing you to manage test dependencies and improve overall test reliability.

Moreover, when two or more methods share the same priority value, TestNG’s default alphabetical ordering provides an additional layer of control, ensuring a consistent and structured execution sequence. This flexibility in ordering, combined with the use of the Test Annotation in TestNG, not only improves test organization but also minimizes the risk of dependency-related test failures, enhancing the overall stability of your automated test suite.

For testers and developers working on complex projects, TestNG’s priority feature along with the Test Annotation streamlines the testing process, making it easier to manage and optimize test flows. With careful use of priority settings and Test Annotation, teams can maintain more robust, reliable tests that support seamless development and faster debugging.

Call to Action

Ready to take control of your test execution order? At H2K Infosys, our expert-led Selenium courses cover TestNG’s @Test Priority in detail, equipping you with the skills to manage complex test flows, prioritize critical tests, and boost test efficiency. Join H2K Infosys today and unlock the full potential of TestNG @Test Priority in Selenium to advance your career in automation testing!

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class