How to prioritize Selenium Test Cases using TestNG Feature

HOME

TestNG provides a number of features like prioritizing the test cases, grouping the test cases, skipping the test cases , defining dependencies between test cases and so on. In this tutorial, we will see how the test cases can be prioritize using TestNG.
If no priority assigned to a Test Case, then the annotated test methods are executed as per the alphabetical order of the tests. Below is an example

 
import org.testng.annotations.Test;

public class TestNGPriorityDemo {


     @Test
     public static void FirstTest() {
           System.out.println("This is Test Case 1");
     }


     @Test
     public static void SecondTest() {
           System.out.println("This is Test Case 2");
     }


     @Test
     public static void ThirdTest() {
           System.out.println("This is Test Case 3");
     }


     @Test
     public static void FourthTest() {
           System.out.println("This is Test Case 4");
     }
}

Output
This is Test Case 1
This is Test Case 4
This is Test Case 2
This is Test Case 3
PASSED: FirstTest
PASSED: FourthTest
PASSED: SecondTest
PASSED: ThirdTest
===============================================
    Default test
    Tests run: 4, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0

In the above example, FirstTest and FourthTest have the highest preference as per the alphabetical order. F comes before S and T. FirstTest is executed before Fourth because preference of I is higher than O     The methods can be prioritized by assigning a number to the annotated test cases. Smaller the number, higher the priority. Priority can be assigned as parameters while defining the test cases. In the below example, we have assigned priority to test cases and now they are executed as per the priority. The Test Case with priority = 1 has the highest precedence. It override the rule of executing test cases by alphabetical order.

To Run the TestNG program, right click on the program, select Run As TestNG Test

import org.testng.annotations.Test;

public class TestNGPriorityDemo {


     @Test(priority = 3)
     public static void FirstTest() {
           System.out.println("This is Test Case 1, but after priority Test Case 3");
     }


     @Test(priority = 4)
     public static void SecondTest() {
           System.out.println("This is Test Case 2, but after priority Test Case 4");
     }


     @Test(priority = 2)
     public static void ThirdTest() {
           System.out.println("This is Test Case 3, but after priority Test Case 2");
     }


     @Test(priority = 1)
     public static void FourthTest() {
          System.out.println("This is Test Case 4, but after priority Test Case 1");
     }
}

Output
This is Test Case 4, but after priority Test Case 1
This is Test Case 3, but after priority Test Case 2
This is Test Case 1, but after priority Test Case 3
This is Test Case 2, but after priority Test Case 4
PASSED: FourthTest
PASSED: ThirdTest
PASSED: FirstTest
PASSED: SecondTest
===============================================
    Default test
    Tests run: 4, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0


===============================================

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s