How to set test execution order in TestNG

In this section, we are going to understand how to assign different priorities to Different Testcase in our Java Program. and how to set test execution order in TestNG but Before Prioritizing Testcases in TestNG Follow basic steps on How to install the TestNG Plugin in Eclipse IDE.

In TestNG, we can Assign Priorities to TestCases in Java Program based on that sequence the execution takes place Generally the Testcase which is not assigned any Priority is considered as zero Priority which would be always executed before one Priority and other priorities will be followed.

Set test execution order in TestNG

Create a Class Name Priority and create Four Testcases assigned with Prints Statements to Display the Execution Flow and assign Priority to each Testcase in class based on Requirement.

Code

import org.testng.annotations.Test;

public class Priority {
    @Test //no Priority is assigned to it so the default zero would be assigned to it 
     public void TestOne()
     {
          System.out.println("This is TestOne ");
     }
     @Test (priority =1)//Priority one is set to Testcase
     public void TestTwo()
     {
          System.out.println("This is TestTwo ");
     }
     @Test (priority =2)//Priority Two is set to Testcase
     public void TestThree() 
     {
          System.out.println("This is TestThree ");
     }
     @Test (priority =3)//Priority Three is set to Testcase
     public void TestFour() 
     {
          System.out.println("This is TestFour ");
     }
}

Code Explanation

@Test 
public void TestOne() 
{
System.out.println("This is TestOne ");
}

In the example above, no Priority is Assigned to Testone(), so the Default Priority That is Zero would be Assigned to it.

@Test (priority =1)
public void TestTwo() 
{
System.out.println("This is TestTwo ");
}

In the example above,(priority =1)is Assigned to TestTwo()Followed by Priority 2 and 3 Which are Assigned to TestThree and TestFour Test cases.

Output

We Have Successfully Prioritized Tests cases in TestNG.