In the article about running our first test case in TestNG, we learned how test cases need to be alphabetically for a serial sequential run or else they could execute out of our will. A common problem here is that we cannot work with this flow all the time. Suppose I have a test called ProductTest, which we define before the ConsumerTest, but I want ProductTest to run first because the final result can only evaluate if ProductTest runs first and provides values to ConsumerTest. I need to always think of something lexicographic in order even though it does not make any sense. It is where we harness the power of TestNG Test Priority and Test Sequence, and this post is about that. The table of contents for this post are:

  • What Is Prioritization In TestNG?
    • How to give Priority in TestNG Test?
    • How To Run Prioritized Tests In TestNG Using Selenium?
  • Same Priority Tests In TestNG
    • Analyzing Test Sequence with Test Priority in TestNG with Selenium
  • How To Skip A Test Case In TestNG?

What Is Prioritization In TestNG?

Prioritization in TestNG is a way to provide a sequence to the methods so that they do not run out of order. Since alphabetically running test cases in TestNG have no logical sequence (concerning the tests and code), providing priority to these test cases helps us managing our tests' execution.

Priority in TestNG test cases is a parameter with attribute value as "priority".

How to give Priority in TestNG test?

The following is the syntax for allocating a priority to a test case method.

@Test (priority = 1)
public void func(){
   //test code
}

Here the test method func has a priority of 1.

It is important to note a couple of points regarding priority in TestNG:

  • Definition of Priority in TestNG test methods can only be the @Test methods.
  • Lower the priority number; higher is the priority of the test case method.
  • Priority in TestNG contains only integer value. The value can be negative, zero, or positive.
  • If a tester defines a priority in decimal in TestNG, it needs to convert first to Integer (through typecasting).
  • One method is allowed to have only one priority in TestNG.
  • Priority cannot pass through the XML files.

Keeping these points in mind, we are ready to run our first test with declared priority methods using selenium.

How To Run Prioritized Tests In TestNG Using Selenium?

Writing a test case with priority in TestNG is similar to how we write a typical test case in TestNG but with a "priority" attribute.

Observe the following code, which has two methods: OpenBrowser and CloseBrowser.

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

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

	@Test (priority = 1)
	public void CloseBrowser() {
		driver.close();
		System.out.println("Closing Google Chrome browser");
	}

	@Test (priority = 0)
	public void OpenBrowser() {
		System.out.println("Launching Google Chrome browser"); 
		driver.get("https://www.demoqa.com");
	}
}

In the OpenBrowser method, I am trying to open the browser and enter the URL "www.demoqa.com." The "CloseBrowser" method, however, is used to close the driver. The priorities set are 0 for OpenBrowser and 1 for CloseBrowser, so I expect the OpenBrowser method to run first.

Execute the above TestNG test file to check the output. testng priority test case

As expected, the OpenBrowser method ran first because of a lower priority. Had I not declared the priority here, it would have run alphabetically, i.e., CloseBrowser first and then OpenBrowser.

Tests with Same Priority in TestNG

If priority is deciding the sequence of tests in TestNG, then a simple question arises in our minds: what if I declare the same priority to all the tests in TestNG? Let' see this case by observing the following code:

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

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

	@Test (priority = 1)
	public void CloseBrowser() {
		driver.close();
		System.out.println("Closing Google Chrome browser");
	}

	@Test (priority = 0)
	public void OpenBrowser() {
		driver.get("https://www.demoqa.com");
		System.out.println("Launching Google Chrome browser"); 
	}

	@Test (priority = 1)
	public void AccountTest(){
		System.out.println("Some tests for Customer Account");
	}
}

In the above code, I have set the priority of AccountTest and CloseBrowser as one while OpenBrowser retains its priority of 0. Run this code to see the output: testng same test priority

The OpenBrowser ran first, no questions there!! But, an interesting pattern appears in the methods containing similar priorities (1). Even though the CloseBrowser method was declared first, AccountTest was the one that ran before CloseBrowser. A good explanation for this behaviour is that if two or more methods have the same priorities in TestNG, then their running test sequence is alphabetic. Since "A" comes before "C", the method AccountTest ran first.

But let's wait and think at this moment for a second by recollecting the memories of test cases and priorities. If all the test cases with no priorities run alphabetically and all the test cases with similar priorities also run alphabetically, then there must be some relation between these two situations.

Can you guess it by shooting in the air?

The test methods with no priority assigned have a default priority equal to 0. It means if we define no priorities, all the test methods will be assigned priority 0, and a similar priority case will apply. Let's prove this fact with some test code in the next section.

Analyzing Test Sequence with Test Priority In TestNG with Selenium

The below-given code is the same as the code we used above. But this time, I have reassigned the priorities of all the methods.

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

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

	@Test (priority = 0)
	public void CloseBrowser() {
		driver.close();
		System.out.println("Closing Google Chrome browser");
	}

	@Test (priority = -1)
	public void OpenBrowser() {
		System.out.println("Launching Google Chrome browser"); 	        
		driver.get("https://www.demoqa.com");
	}

	@Test
	public void AccountTest(){
		System.out.println("Some tests for Customer Account");
	}
}

In the above test code, the method OpenBrowser contains priority as -1, CloseBrowser as 0, and no priority assignment happens to AccountTest. Let's see the output after running the above selenium code in Eclipse. testng test priority analyze

Looking at the output of this test code, we prove three main points in TestNG priority:

  1. We can assign negative priorities to a method.
  2. With a method with no priority, the priority is set to 0 by default.

Observe that the AccountTest method ran before CloseBrowser even without having any priority because both sets to priority = 0, and hence, they run alphabetically.

Hence, we can change the sequence of tests in TestNG using priorities.

How to Skip tests in TestNG using Parameters?

In this tutorial, until now, we learned that we could give priority to the test cases and change the sequences of the test methods execution. If we do not, TestNG assigns the priority as zero. But, still, with or without the priority, the method will execute. Often, we are required just to skip a test case method and perform testing. It is skipping, and we carry it through the "enabled" parameter.

Let's see the following code meant to skip the test CloseAccount.

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

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

	@Test (priority = 0)
	public void CloseBrowser() {
		driver.close();
		System.out.println("Closing Google Chrome browser");
	}

	@Test (priority = -1)
	public void OpenBrowser() {
		System.out.println("Launching Google Chrome browser"); 	        
		driver.get("https://www.demoqa.com");
	}

	@Test (enabled = false)
	public void AccountTest(){
		System.out.println("Some tests for Customer Account");
	}
}

Execute the above test case code and see the output. 2_skip_test_case

There you go. We have skipped a test case and broke the typical sequence of the test case in TestNG.

Conclusively, it was all from my side in this tutorial about priorities and sequencing in TestNG. Test priorities are very useful in running the code in the sequence we want with minimum to no changes in the code part, like shuffling the functions. Additionally, it is a light yet powerful topic in TestNG. Subsequently, we will move on to our next tutorial now.

Common Questions On Priority In TestNG

How do you give priority in TestNG?
A tester can provide a priority value to the test case by defining the priority parameter with @Test annotation. Moreover, if there is no priority defined, the default priority is zero (0) for that test case.

Can we give a negative priority in TestNG?
Negative priorities are acceptable in TestNG. However, you can provide an integer value to the priority parameter, including zero.

TestNG DataProviders
TestNG DataProviders
Previous Article
TestNG Reporter Log
TestNG Reporter Log
Next Article
Harish Rajora
I am a computer science engineer. I love to keep growing as the technological world grows. I feel there is no powerful tool than a computer to change the world in any way. Apart from my field of study, I like reading books a lot and developing new stuff.
Reviewers
Lakshay Sharma's Photo
Lakshay Sharma

Similar Articles

Feedback