Saturday, August 26, 2017

What is priority in TestNG and How to use it ?

Priority is an attribute used along with @Test annotation to set the priority of a test case. Using priority user can define the order of the execution of test cases (@Test methods) in a java class. Some points on priority in TestNG are below:
  • The default priority of any test case (@Test method) is zero. That is highest priority.
  • Higher the priority value, lower is the priority. In test set (java class) @Test (priority=0) highest priority, @Test (priority=1) second priority, @test (priority=2) third priority and so on.
  • If no priority is defined, all the test cases are having zero priority. In this case the test cases will be executed in alphabetical order.
Real Time Scenario: There could be situations where we need to execute the test cases in a sequence to achieve a task, suppose I have a scenario sending an email. To do this task the sequence of test execution will be 1) launching application, 2) login to account, 3) compose email and send, finally 4) logout.  So here prioritizing the test cases is required. Let us do this…

Step 1: Write the test cases as shown in the below screenshot, no priority is defined for them. Since no priority has been defined all are having same priority (zero), so the test cases will be executed in alphabetic order.

Run the test cases and check the results.

You can observe from the above screenshot that, test cases are not executed in the expected sequence. 
Step 2: Now set the priorities to the test cases as show in the below screenshot and achieve the task.

Run the test cases and check the results.

You can observer in the above screenshot that, test cases are executed in the expected sequence. This how we can prioritize the test cases using priority attribute.

Please comment your queries and suggestions on this topic. Thanks for reading!!

#seleniumbabu #HappyLearning 

No comments:

Post a Comment

DevOps 01: What is DevOps and How it benefits organizations?

DevOps is a culture in an organization, where the development team and operations team help each other by sharing information, process and t...