Open In App

How to set priority to the test cases in TestNG?

Last Updated : 30 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article
Article Tags :

Similar Reads