• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Selenium » Free Selenium Tutorial » Priority Attributes of @Test Annotation TestNG

Priority Attributes of @Test Annotation TestNG

Last Updated on: November 30, 2019 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Priority Attributes In TestNG
  • Priority Attributes Execution Sequence Order
  • Things To Remember About Priority Attributes

Priority Attributes In TestNG

When we convert the test case to test script, most of the time, we 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.

For example:

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”);
   }
}

When we have executed a class, then methods of that class are not executed in the same orders in which they are mention in that class. Because TestNG sort the methods on ASCII value of the method name char by char in a method name and arrange them in ascending order.

Priority Attributes Execution Sequence Order

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

package PriorityExamples;
import org.testng.annotations.Test;
public class PriorityExample 
{

   @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");
   }
}

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

  • ‘$’ has the highest priority.
  • Capital alphabets will be second priority.
  • Underscore will have 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 the handle, such scenarios TestNG provide a Priority Attributes with inside the @Test annotation. By mentioning the priority value using Priority Attributes, we can execute those methods as per our needs.

Things To Remember About Priority Attributes

  • You need to mention the “priority” in the small letter as it is case sensitive.
  • Priority Attributes is applicating for those methods which are annotated by @Test
  • The Priority value should be an integer value. That may ve a negative, zero, or a 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, then testNG will decide the priority based on the ASCII value.

Sample Program on :

package PriorityExamples;
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");
   }

}

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

package PriorityExamples;
import org.testng.annotations.Test;
public class PriorityAttributeExample 
{
   @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");
   }

}

The below program, we will see hows the execution flow when multiple methods are set with the same priority:

package PriorityExamples;
import org.testng.annotations.Test;
public class PriorityAttributeExample 
{
   @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");
   }

}

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

Superclass:

package PriorityExamples;
import org.testng.annotations.Test;
public class SuperTestNGClass
{
   @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 PriorityExamples;
import org.testng.annotations.Test;
// Inheritance
public class PriorityExample extends SuperTestNGClass 
{

   @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");
   }
}

We know when we inherit another class in TestNG also all the methods of the superclass are inherited to the child class. 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. And the sorting order is the First subclass method executed after those superclass methods got executed.

I hope after reading the article about Priority Attributes, you can learn how you can use the Priority Attributes as per your requirement. We also know how you are using these Priority Attributes when you writing the automation script.

How to TestNG Priority Annotation Works In Selenium Example?

package com.selenium.TestNG;
import org.testng.Assert;
import org.testng.annotations.Test;
public class PriorityTest
{
   @Test(priority=1)
   public void login()
   {
      System.out.println("Login");
   }
   @Test(priority=2)
   public void search()
   {
      System.out.println("Search");
      Assert.assertEquals("Manoj", "Monoj");
   }
   @Test(priority=3)
   public void advsearch()
   {
      System.out.println("Adv Search");
   }
   @Test(priority=4)
   public void logout()
   {
      System.out.println("Logout");
   }
}

Source: Link

    Filed Under: Free Selenium Tutorial

    Reader Interactions

    Comments

    1. Avatar for samsonsamson says

      May 15, 2020 at 4:52 AM

      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
      • Avatar for Softwaretestingo Editorial BoardSoftwaretestingo Editorial Board says

        May 16, 2020 at 7:01 PM

        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 Reply Cancel reply

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

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers