 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Control Test Execution Order in TestNG?
A TestNG class can have different tests like test1, test2, test3 etc. Once a user runs the TestNG class consisting of various tests, it runs the test cases in alphabetically order based on the name provided. However, user can assign the priority to these tests so that these tests can run as per user's priority. Priority starts from 0 and be in incremental order. Priority 0 takes the highest priority and decreasing the priority when priority gets increase as 1,2, 3 etc.
In this article, let's analyse how to control the order of execution in different ways.
Scenario 1
If test2 (priority=0), test1(priority=1), test3(priority=2) then test2 will run first and after that test1 and so on based on priority.
Approach/Algorithm to solve this problem
- Step 1: import org.testng.annotations.Test for TestNG. 
- Step 2: Write an annotation as @test 
- Step 3: Create a method for the @test annotation as test1 and provide the priority=1. 
- Step 4: Repeat the steps for test2 and test3 for priority 0 and 2 respectively. 
- Step 5: Now create the testNG.xml. 
- Step 6: Now, run the testNG.xml or directly testNG class in IDE or compile and run it using command line. 
Example
The following code to create a TestNG class and displays the priority order of execution:
import org.testng.annotations.Test; public class OrderofTestExecutionInTestNG { @Test(priority=1) public void test1() { System.out.println("Starting execution of TEST1"); } @Test(priority=0) public void test2() { System.out.println("Starting execution of TEST2"); } @Test(priority=2) public void test3() { System.out.println("Starting execution of TEST3"); }  Output
Starting execution of TEST2 Starting execution of TEST1 Starting execution of TEST3
Scenario 2
If test2 (priority=0), test1(priority=1) and test3 has no priority then test2 will run first and after that test3 and in the last test1. Since test3 doesn't have user defined priority, TestNG assigns it to as priority=0 and in alphabetically test2 comes first and then test3.
Approach/Algorithm to solve this problem
- Step 1: import org.testng.annotations.Test for TestNG. 
- Step 2: Write an annotation as @test 
- Step 3: Create a method for the @test annotation as test1 and provide the priority=1. 
- Step 4: Repeat the steps for test2 and test 3 for priority 0 and don't provide any priority respectively. 
- Step 5: Now create the testNG.xml. 
- Step 6: Now, run the testNG.xml or directly testNG class in IDE or compile and run it using command line. 
Example
The following code to create a TestNG class and displays the priority order of execution:
import org.testng.annotations.Test; public class OrderofTestExecutionInTestNG { @Test(priority=1) public void test1() { System.out.println("Starting execution of TEST1"); } @Test(priority=0) public void test2() { System.out.println("Starting execution of TEST2"); } @Test() public void test3() { System.out.println("Starting execution of TEST3"); }  Output
Starting execution of TEST2 Starting execution of TEST3 Starting execution of TEST1
