Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
December 16, 2016

Prioritizing Tests in TestNg





Prioritizing tests in testng will discuss about the order of execution tests in test suite. We will write test methods our own way using @Test annotation. After writing the test cases we will execute the test cases either normally or from the testng.xml file. After execution of the tests if we observe the order of execution then we can find that tests executed by taking the alphabetical order. And all the tests will have the equal priority as we did not set any priority to the tests.

In order to set the priority to the tests then we can use one of the @Test attributes called “Priority”. By default all the tests will have the same priority called Zero(i.e. If you not set any priority then it will take the priority as Zero).
If you give the priority equal to all the test cases then all the tests will execute in alphabetical order. Here we will see how the priority will work while executing the tests by taking few examples:
1. In the below example we will see test case execution flow without setting any priority to the tests (i.e. all the tests will have the priority as ZERO).

public class WithOutPriority
{
    @Test
    public void secondTest()
    {
        System.out.println("In secondTest method");
    }
    
    @Test
    public void thirdTest()
    {
        System.out.println("In thirdTest method");
    }
    
    @Test
    public void fourthTest()
    {
        System.out.println("In fourthTest method");
    }
}

Below is the Output for the same:

1

If we observe the above output of all the 3 methods they executed in the alphabetical order. We have the order in test class is secondTest, thirdTest and fourthTest. But after execution the output is fourthTest, secondTest and thirdTest.

2.In the below example we will see test case execution flow with Priority attribute. Will take three test methods and will set the priority to them like one is without priority attribute and remaining

with 1 and 2.

public class PartialPriority
{
    @Test(priority = 1)
    public void secondTest()
    {
        System.out.println("In secondTest method");
    }
    
    @Test
    public void thirdTest()
    {
        System.out.println("In thirdTest method");
    }
    
    @Test(priority = 2)
    public void fourthTest()
    {
        System.out.println("In fourthTest method");
    }
}

Below is the Output for the same:

2

If we observe the above output of all the 3 methods they executed as per the priorities we set. The test without priority executed first as the default priority is equal to ZERO and it takes the high priority. And then priority = 1 will execute and last priority = 2 will execute.

3.In the below example we will see test case execution flow with Priority attribute. Will take three test methods and will set the priority to them like 1,2 and 3

public class PriorityTest
{
    @Test(priority = 1)
    public void secondTest()
    {
        System.out.println("In secondTest method");
    }
    
    @Test(priority = 3)
    public void thirdTest()
    {
        System.out.println("In thirdTest method");
    }
    
    @Test(priority = 2)
    public void fourthTest()
    {
        System.out.println("In fourthTest method");
    }
}

Below is the Output for the same:

3

If we observe the above output of all the 3 methods they executed as per the priorities we set. The test with priority = 1 will execute first and priority = 2 will execute second and priority = 3 will execute last.
This way we can prioritize the tests in testNG to control the execution flow.
Please watch You tube video for better understanding.



Share this post: on Twitter on Facebook

Execute Multiple XML files in TestNG Preserve Order in TestNG

Related Posts

Excel to DataProvider

TestNG

Read data from Excel to DataProvider in Selenium

Running TestNG programmatically

TestNG

Running TestNG Tests Programmatically

EXECUTING ONLY FAILED TESTS

TestNG

Executing Only Failed Tests in TestNG

Capture Screenshot for Failed Tests

TestNG

Capture Screenshot for Failed Tests in TestNG

Preserve Order in TestNG

TestNG

Preserve Order in TestNG

EXECUTE MULTIPLE XML FILES

TestNG

Execute Multiple XML files in TestNG

CUSTOM REPORTER IN TESTNG (1)

TestNG

Custom Reporter in TestNG

CUSTOM LOGGER IN TESTNG

TestNG

Custom Logger in TestNG

ASSERTIONS

TestNG

TestNG Assertions

PARALLEL EXECUTION OF classes

TestNG

Parallel Execution of Classes In TestNG

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025