How to set priority to the test cases in TestNG?
Last Updated :
30 Aug, 2024
TestNG – Test Next Generation – is a testing framework based on the JUnit and NUnit, but with added functionality. It is designed to cover all categories of tests: The test types include unit, functional, end-to-end and integration tests. When it comes to organizing the test cases, the usage of TestNG is very flexible which allows more control over dependencies and the order of their execution.
Why set priority to test cases?
Usually, in a test suite, some tests must be executed before other tests. For example:
- Setup tests have to be executed before any feature tests intending to exercise a feature of our application.
- Critical path tests should run soon to detect severe issues as soon as possible.
- Smoke tests need to be executed before other tests to ensure that the identified functions are operational.
Prioritization aids the management of the order or sequence of tests to be conducted with an emphasis on the key or basic tests that are conducted first.
How to Set Priority in TestNG
There is a provision for assigning the priority of a test case through the '@Test' annotations in TestNG. As it is with other test frameworks, TestNG does not necessarily execute the test cases in the order they are written. Still, there is a way to control the order of test execution while using the '@Test' annotation – it is the priority attribute.
Java
@Test(priority = 1)
public void testMethod() {
// Test logic here
}
Here, 'priority = 1' means that this test should be run first among all the created tests. That is why the lower the priority number the higher the chance for the test to run first. TestNG runs the tests from the lower level of priority to the high level of priority.
Example: Prioritizing of test cases
Now that we know how to set priority in TestNG let us consider an example of how it can be done.
Java
import org.testng.annotations.Test;
public class PriorityTest {
@Test(priority = 1)
public void loginTest() {
System.out.println("Executing Login Test");
// Login test logic
}
@Test(priority = 2)
public void dashboardTest() {
System.out.println("Executing Dashboard Test");
// Dashboard test logic
}
@Test(priority = 3)
public void logoutTest() {
System.out.println("Executing Logout Test");
// Logout test logic
}
}
Explanation:
loginTest is the most important ('priority Equals=1'), thus it will run first.
- dashboardTest this script will be the second to run ('priority = 2').
- logoutTest has the lowest priority ('priority = 3') of all the given tests and, therefore, will be executed after all the others.
Output:
Executing Login Test
Executing Dashboard Test
Executing Logout Test
5. Common Mistakes and Best Practices
Common Mistakes:
- Missing Priorities: If no priority is mentioned, TestNG provides the default value to it as '0'. When no priorities are set for those tests, the tests will run in the order of their establishment before proceeding to the tests for which established priorities exist.
- Duplicate Priorities: If the priorities are the same for two or more tests, TestNG will be executed in the order as defined in code which cannot be predicted.
Best Practices:
- Consistent Priority Setting: If the order of the tasks’ execution is critical, then it is always important to explicate priorities.
- Avoid Negative Priorities: As mentioned before, working with negative priorities might bewilder. Avoid mixed numbers and fractions which make it difficult for the reader to understand the computation.
- Use Priority Sparingly: This is done to ensure that only as many priorities as needed are set in order to have clean and easily comprehensible and sustainable test code.
Conclusion
Yet another basic yet effective and very useful feature of TestNG is setting priority to test cases where priority is set to decide the flow of execution. This eliminates a situation whereby some important tests may be left behind in the event that a large number of tests are run; this becomes possible through priorities.
The right approach towards test case prioritization can enhance the pace of testing and come across with major problems much earlier and hence enhances the effectiveness of the testing process.
Similar Reads
How to Create Test Suite in TestNG? In this article we will learn about test suites and how can we build them using Java and TestNG. TestNG is a modern testing framework that is used very widely used today. It has a broad base of features that allow us to write unit tests and club them together in groups. Table of Content What is a Te
11 min read
How to Write Test Cases - Software Testing Software testing is known as a process for validating and verifying the working of a software/application. It re-check that the software functions are meets the requirements without errors, bugs, or any other issues and provides the expected output to the user. The software testing process is not li
15+ min read
What is the Best Practice to Skip a Test in TestNG? TestNG is a popular testing framework in the Java ecosystem, designed to cover a wide range of test categories, including unit, functional, end-to-end, and integration tests. While writing tests, there are scenarios where you might want to skip certain test cases. This could be due to incomplete fea
5 min read
How to get a list of all the test methods in a TestNG class? In order to get all the test methods in a TestNG class, one can use the ITestContext or IAnnotationTransformer interfaces that are available in TestNG. The below example uses the ITestContext interface and allows access to the test methods while the test is running.Approach to Listing All Test Metho
3 min read
How to retrieve the test suite name at runtime in TestNG? Sometimes in TestNG it's necessary to retrieve the name of the test suite at runtime. This is especially true when generating reports, doing logging, or debugging. The ITestContext interface provides various methods to get details about the current test run. Approach to Retrieve Test Suite Name at R
2 min read
How to run multiple test classes in TestNG? TestNG makes it easy to run multiple test classes in one go, which helps test different parts of an application efficiently. By running multiple classes together, you save time and ensure more thorough testing. You can run these classes using a TestNG XML file, directly from Eclipse, or through the
2 min read
Test Case Prioritization in Software Testing As the name suggests, test case prioritization refers to prioritizing test cases in the test suite based on different factors. Factors could be code coverage, risk/critical modules, functionality, features, etc. Table of Content Why should test cases be prioritized?Prioritization TechniquesMetric fo
5 min read
How to run multiple test cases using TestNG Test Suite in Selenium? In Selenium automation testing, efficiently managing and executing multiple test cases is crucial. TestNG, a powerful Java checking-out framework, offers an excellent answer to this. Using TestNG, you can group tests, create test units, and keep them organized. This article guides you through the st
4 min read
Severity in Testing vs Priority in Testing In software testing, a bug is the most critical entity. The most important attributes that can be assigned to a bug are priority and severity. They help teams to efficiently fix bugs and go through the release scheduling processes without letting any critical issues fall through the gaps. The articl
6 min read
What is the order of test execution with priority in TestNG? In TestNG, controlling the order of test execution is crucial for ensuring that your tests run in a logical sequence, especially when certain tests depend on others. The priority attribute in TestNG allows developers to define the order of execution explicitly. A lower priority value means the test
4 min read