Last updated on October 10th, 2019 at 09:39 am
We have seen how a TestNG class executes methods when we do not provide any order to methods. You can refer those posts here:
But, you may require to execute methods in a certain order. Consider a scenario where you need to register a user and then same user needs to perform some actions. You write two test methods in a TestNG class say “RegisterUser” and “PerformSomeAction”. These methods must be run in order. First “RegisterUser” followed by “PerformSomeAction” method. If we do not specify any order, TestNG will execute “PerformSomeAction” before “RegisterUser” and it will not serve your purpose. You must need to specify order explicitly. For this TestNG provides an attribute called “Priority”.
- You need to write this element in small letters as “priority”.
- Priority is an element applicable only for @Test annotated methods.
- Priority should be an integer value.
- It can be negative , zero or positive number. If you write it is decimal, you must need to cast it into integer.
- TestNG will execute test methods from lowest to highest priority. Remember Lower priorities will be scheduled first.
- TestNG ignore default priority based on ASCII if priority value is provided.
- You can pass duplicate priority to test methods. In case of tie, TestNG will decide priority based on ASCII value.
- You can create a TestNG class with some test methods with priority and some without priority in same class.
- Test methods without priority will have default priority of Zero and execution sequence will be decided based on ASCII value.
- Suppose if you have three test methods with first two test methods say M1, M2 with priority as -1 and 2 respectively and third test methods say M3 without any priority. In this case, TestNG will have priority as M1=-1, M3=0 and M2=2. So FIrst M1 will be executed followed by M3 and M2.
- You can not define multiple priority element for a test method.
- You can not pass priority to methods through testng.xml.
Scenario 1: All test methods with priority. Apply Lower priorities will be scheduled first concept.
Scenario 2: Mixed test methods with priority and without priority
Scenario 3: A class with duplicate test priorities methods
More about TestNG in upcoming posts. Stay tuned.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
Author: Amod Mahajan
My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning