Make Selenium Easy

TestNG Tutorials 18: Default Priority Of @Test Methods In TestNG

Hello Folks,

Let’s run below TestNG class and observe output:

Output:

Can you see different order of execution of test methods than it has appeared in class? “Amrrod” method is last but got executed first. “Print” method is first but executed in second position.

Actually, TestNG does not run Test methods as it appears in TestNG class (Until we use some attributes like priority etc). It sorts method names based on ASCII value of method name char by char in a method name and arrange them in ascending order. You can find ASCII value of Unicode characters here. ASCII values of “a”, “p” and “s” are “61”, “70”, and “73”. So order will be “amrrod”, “print” and “show” methods.

Let’s see some more:

We can define a method name starting with underscore(“_”) or dollar (“$”) or both. Examples are:

_Method , $Method, _$Method, $_Method 

How TestNG will priorities these method? It follows below rule based on ASCII value.

  1. ‘$’ has highest priority.
  2. Capital alphabets will be second priority.
  3. Underscore will have third priority.
  4. Small letter alphabets will be fourth priority.

See an example below:

Output:

 

Liked the post? If yes please share and let others also learn.

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.
#ThanksForReading
#HappySelenium

Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

3 thoughts on “TestNG Tutorials 18: Default Priority Of @Test Methods In TestNG

  1. This is best tutorial! I was asked thos question in an interview and since then I was looking for the answers. Finally found it.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.