Priority Attributes of @Test Annotation TestNG

Priority Attributes In TestNG: When we convert the test case to a test script, we usually categorize them according to their task or behavior and put the number of tests in a single class to run all in a single shot.

package PriorityExamples;
import org.testng.annotations.Test;
public class PriorityExample 
{
   @Test
   public void printMethod() 
   {
      System.out.println(“Print method”);
   }

   @Test
   public void showMethod() 
   {
      System.out.println(“Show method”);
   }

   @Test
   public void amroodMethod() 
   {
      System.out.println(“Amrrod method”);
   }
}

Output:

Amrrod method
Print method
Show method

When we have executed the above class, then the methods of that class are not executed in the same order in which they are mentioned in that class. Because TestNG sorts the test methods based on the ASCII value of the method names char by char and arranges them in ascending order.

Priority Attributes Execution Sequence Order

As mentioned, testNG makes the methods in an order based on the ASCII value. So let us try to learn about bit details:

package com.softwaretestingo.testng.attributes;
import org.testng.annotations.Test;
public class ExecutionOrderWithSpecialCharacter 
{
	@Test
	public void PrintMethod() 
	{
		System.out.println("Capital P method");
	}

	@Test
	public void printMethod() 
	{
		System.out.println("Small P method");
	}

	@Test
	public void _Method() 
	{
		System.out.println("Underscore method");
	}

	@Test
	public void $Method() 
	{
		System.out.println("Dollar method");
	}
}

Output:

Dollar method
Capital P method
Underscore method
Small P method

As we have seen in the output, TestNG gives high priority to the $ and low priority to small letter alphabets. Here is the order:

  • ‘$’ has the highest priority.
  • Capital alphabets will be the second priority.
  • Underscore will have a third priority.
  • Small letter alphabets will be the fourth priority.

But sometimes, we want to execute those methods in a specific order, or we have to give precedence to certain test methods over others.

For handling such scenarios, TestNG provides Priority Attributes inside the @Test annotation. We can execute those methods per our needs by mentioning the priority value using Priority Attributes.

Things To Remember About Priority Attributes

  • You must mention the “priority” in the small letter as it is case-sensitive.
  • Priority Attributes are applied for those methods that @Test annotates.
  • The Priority value should be an integer value. That may be a negative, zero, or positive value.
  • If priority is set for the methods, then the execution will start from lowest to highest priority.
  • If multiple methods have the same priority value, testNG will decide the priority based on the ASCII value.
package com.softwaretestingo.testng.attributes;
import org.testng.annotations.Test;
public class PriorityAttributeExample 
{
	// We can pass priority as negative value.
	   @Test(priority=-1)
	   public void NegativePrioirty()
	   {
	      System.out.println("Negative Prioirty");
	   }

	   // We can pass priority as default value zero.
	   @Test(priority=0)
	   public void ZerothPriority()
	   {
	      System.out.println("Zeroth Priority");
	   }

	   @Test(priority=1)
	   public void PositivePriority()
	   {
	      System.out.println("Positive Priority");
	   }

	   // It is not mandatory to pass priority in a order
	   @Test(priority=100)
	   public void SkippedPriority()
	   {
	      System.out.println("Skipped Priority");
	   }
}

Output:

Negative Prioirty
Zeroth Priority
Positive Priority
Skipped Priority

Let us understand the execution flow when a few methods are set with priority and some without priority.

package com.softwaretestingo.testng.attributes;
import org.testng.annotations.Test;
public class PriorityAttributeExample2 
{
	@Test(priority=-1)
	public void M1withPriority()
	{
		System.out.println("M1withPriority");
	}

	// TestNG will assign priority as zero to this method
	@Test
	public void PrintWithoutPriority()
	{
		System.out.println("PrintWithoutPriority");
	}

	// TestNG will assign priority as zero to this method
	/*
	 * There is tie between PrintWithoutPriority and DispWithoutPriority.
	 * So TestNg will use here ASCII rule to decide which method to be run first.
	 * Since ASCII of 'D' is 68 and ASCII of 'P' is 80. So method DispWithoutPriority
	 * will be executed first as it has lowest ASCII.
	 */
	@Test
	public void DispWithoutPriority()
	{
		System.out.println("DispWithoutPriority");
	}

	@Test(priority=1)
	public void M4withPriority()
	{
		System.out.println("M4withPriority");
	}
}

Output:

M1withPriority
DispWithoutPriority
PrintWithoutPriority
M4withPriority

In the below program, we will see how the execution flows when multiple methods are set with the same priority:

package com.softwaretestingo.testng.attributes;
import org.testng.annotations.Test;
public class PriorityAttributeExample3 
{
	@Test(priority=1)
	public void M1withPriority()
	{
		System.out.println("M1withPriority");
	}

	// TestNG will assign priority as zero to this method
	@Test(priority=3)
	public void DuplicatePriorityMethod1()
	{
		System.out.println("DuplicatePriorityMethod1");
	}

	/*
	 * There is tie between DuplicatePriorityMethod1 and DuplicatePriorityMethod2.
	 * So TestNg will use here ASCII rule to decide which method to be run first.
	 * Both methods have same name just before last character. Since '1' in DuplicatePriorityMethod1
	 * will have lower ASCII then '2' in DuplicatePriorityMethod2. So DuplicatePriorityMethod1
	 * will be run first followed by DuplicatePriorityMethod2.
	 */

	@Test(priority=3)
	public void DuplicatePriorityMethod2()
	{
		System.out.println("DuplicatePriorityMethod2");
	}

	@Test(priority=2)
	public void M4withPriority()
	{
		System.out.println("M4withPriority");
	}
}

Output:

M1withPriority
M4withPriority
DuplicatePriorityMethod1
DuplicatePriorityMethod2

We have discussed in detail how inheritance works with TestNG in detail. If you have not checked that, then you can follow that link. Now, we will see how the default priority works with inheritance.

Superclass:

package com.softwaretestingo.testng.attributes;
import org.testng.annotations.Test;
public class PrioritySuperClass 
{
	@Test
	public void $MethodOfSuper() 
	{
		System.out.println("Dollar Method of super class");
	}
	@Test
	public void AskSuperClass() 
	{
		System.out.println("Capital letter method of super class");
	}
}

Child Class:

package com.softwaretestingo.testng.attributes;
import org.testng.annotations.Test;
public class PriorityChildClass extends PrioritySuperClass
{
	@Test
	public void PrintMethod() 
	{
		System.out.println("Capital P method");
	}

	@Test
	public void printMethod()
	{
		System.out.println("Small P method");
	}

	@Test
	public void _Method() 
	{
		System.out.println("Underscore method");
	}

	@Test
	public void $Method() 
	{
		System.out.println("Dollar method");
	}
}

Output:

Dollar method
Capital P method
Underscore method
Small P method
Dollar Method of super class
Capital letter method of super class

When we inherit another class in TestNG, the child class inherits all the superclass methods. But From the output, we got to know that all the methods of both superclass and subclass methods will not be sorted and prioritized combined.

In contrast, the superclass and subclass methods are prioritized and executed separately. The sorting order is the First subclass method executed after those superclass methods are executed.

After reading the article about priority attributes, I hope you can learn how to use them as per your requirements. We also know how you use these Priority Attributes when writing the automation script.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

2 thoughts on “Priority Attributes of @Test Annotation TestNG”

  1. Hi Team,

    Running Superclass and Childclass why the Superclasses calling twice for each method.
    Please advice

    [RemoteTestNG] detected TestNG version 6.14.3
    Dollar method
    Capital P method
    Underscore method
    Small P method
    Dollar Method of super class
    Dollar Method of super class
    Capital letter method of super class
    Capital letter method of super class
    PASSED: $Method
    PASSED: PrintMethod
    PASSED: _Method
    PASSED: printMethod
    PASSED: $MethodOfSuper
    PASSED: $MethodOfSuper
    PASSED: AskSuperClass
    PASSED: AskSuperClass

    ===============================================
    Default test
    Tests run: 8, Failures: 0, Skips: 0
    ===============================================

    ===============================================
    Default suite
    Total tests run: 8, Failures: 0, Skips: 0
    ===============================================

    Reply
    • Samson can you share with us the test script so that we can get an idea. Bcoz its not clear with the results

      Reply

Leave a Comment