- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the order of test execution with priority 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 an alphabetic order based on the names provided. However, the user can assign the priority to these tests so that these tests can run as per user's priority. Priority starts from "0" which takes the highest priority and as the number moves up, the priority decreases.
In this article, let's analyse how the order of execution with priority works in TestNG.
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 with priority=1.
Step 4 − Repeat the steps for test2 and test3 with priority "0" and "2" respectively.
Step 5 − Now create the testNG.xml.
Step 6 − Run the testNG.xml or run the testNG class directly in IDE or compile and run it using command line.
Example
The following code creates 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 have no priority, then test2 will run first and after that test3 and finally test1. Since test3 doesn't have user defined priority, TestNG assigns its priority=0 and in alphabetical order, 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 with priority=1.
Step 4 − Repeat the steps for test2 and test 3 for priority 0 and don't provide any priority.
Step 5 − Now create the testNG.xml.
Step 6 − Run the testNG.xml or run the testNG class directly in IDE or compile and run it using command line.
Example
Use the following code to create a TestNG class and display 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
- Related Articles
- What is the order of execution of TestNG methods?
- What is the order of execution of tests in TestNG?
- How to set priority to the test cases in TestNG?
- What is the priority of BeforeClass and BeforeTest methods in TestNG?
- How does the execution of a particular test method depend on other test\nmethods in TestNG?
- How to overlook a particular test method from execution in TestNG?
- How to get the test group name in TestNG before and after execution?
- How to incorporate and remove test methods from execution from a\ncollection of test cases in TestNG?
- How to retrieve test method description in TestNG before and after execution?
- How to retrieve test method name in TestNG before and after execution?
- What is TestNG Annotation Order?
- How to set the order of execution for test methods in Cucumber?
- What is Execution order of Collection Runner in Postman?
- What is the order of execution of non-static blocks with respect to a constructor in Java?
- How to achieve parallel execution in TestNG?
