TestNG Test Case Priority

In TestNG "Priority" is used to schedule the test cases. When there are multiple test cases, we want to execute test cases in order. Like First we need to execute a test case "Registration" before login.

In order to achive, we use need to add annotation as @Test(priority=??). The default value will be zero for priority.

If you don't mention the priority, it will take all the test cases as "priority=0" and execute.

If we define priority as "priority=", these test cases will get executed only when all the test cases which don't have any priority as the default priority will be set to "priority=0"

The below screen shot shows a test cases which are not defined with any priority.
TestNG Priority

The below examples shows using the priority for test cases.

As we have not defined the priority for testcase "Registration", it will get executed first and then the other testcases based on priority.

import org.testng.annotations.Test;
public class testNGPriorityExample {
	public void registerAccount()
		System.out.println("First register your account");
	public void sendEmail()
		System.out.println("Send email after login");
	public void login()
		System.out.println("Login to the account after registration");

The below images shows the executed result of the above program.

'Registration' test case will get executed first as we normally do that before login to any of the application.

TestNG Priority

Test Frameworks: 


how to 10 or more no scripts in a single instance...Like we login once and run all the scripts and then logout...i am able to run only 2 scripts 3rd one is giving error.

Hi Abinash, can you please let us know what error you are getting and how are you trying to run,

How to set Group and priority to 1 @Test method?

Is there any way to provide the dynamic name for the testcase method for each execution?

Can we give priority=0 in TestNG ?

Can we give priority in negative values(-1)

how to run 10 or more no of scripts in a single instance...Like we login once and run all the scripts and then logout. my scripts are based on search after log in one search test pass but before second test search finishes it moves to third search test and the whole execution fails. is there any way to sort this please let me know many thanks

Greetings! Very helpful advice within this post! It's the little changes which will make the most significant changes.
Many thanks for sharing!

Is there any range to set priority for @Test annotation?
can we give 'n' number of priority?
for example i am having 500 test cases, can i use @Test(Priority=500)

This will not workout in practical, you cannot start giving Priority 1, Priority2 -- etc to 500 for all the tests.
You should divide these tests accordingly with different classes prioritize only for required cases.

Add new comment