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 {
	@Test
	public void registerAccount()
	{
		System.out.println("First register your account");
	}
	@Test(priority=2)
	public void sendEmail()
	{
		System.out.println("Send email after login");
	}
	@Test(priority=1)
	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: 

Comments

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 ?

Yes,we can give

Can we give priority in negative values(-1)

Yes, we can define priority with negative values.
The lowest number will be given highest priority

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!

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.