Priority & preserve-order in TestNG: How to Execute Tests in a Custom Order

In our Annotations in TestNG post, we understood that if there are multiple @Test annotations in a single class, they are executed alphabetically based on method name. Thankfully, TestNG provides us a better way to order our test cases rather than following a particular naming convention.

Priority Parameter in TestNG

To sort the execution flow of test cases, we can use the 'priority' parameter. You can assign priority starting ‘0‘ – highest priority, to the number of @Test methods you have. Let’s understand this with an example: