Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

TestNG Framework

  • What is TestNG Framework?
  • Advantages and Disadvantages of TestNG:
  • Difference between TestNG And Junit framework
  • What is TestNG Annotations?
  • Install TestNG In Eclipse & IntelliJ?
  •  Hierarchy In TestNG Annotations
  • TestNG’s prioritization
  • TestNG Dependent
  • Reporter Class in TestNG
  • TestNG Reports
  • Assertions in TestNG
  • TestNG Groups
  • TestNG Parameters
  •  Cross-Browser Testing in TestNG
  •  Parallel Testing in TestNG
  • Data Providers
  • TestNG Listeners
  • Rerunning failed tests in TestNG
View Categories
  • Home
  • Tutorials
  • TestNG
  • TestNG Framework
  • TestNG’s prioritization

TestNG’s prioritization

TestNG’s prioritization

TestNG’s prioritization feature gives you fine-grained control over the order in which your @Test methods run. By assigning a numeric priority to each test, you can ensure that setup or core‐scenario tests fire before dependent or cleanup tests—even if they’re declared in a different order in code.

1. What Is Prioritization in TestNG?

Prioritization is the mechanism by which TestNG orders test method execution based on an integer value you supply via the priority attribute on @Test. Lower values run first; higher values run later.

@Test(priority = 0)   // runs earliest

@Test(priority = 5)   // runs after any priority <5

@Test               // no priority ⇒ defaults to 0

2. How to Give Priority in TestNG Tests

Simply add priority = <int> to your @Test annotation:

import org.testng.annotations.Test;

public class PriorityExample {

  @Test(priority = 1)
  public void initialize() {

    System.out.println("1- initialize");

  }

  @Test(priority = 3)
  public void cleanup() {

    System.out.println("3- cleanup");

  }

  @Test // defaults to priority = 0
  public void defaultPriority() {

    System.out.println("0- defaultPriority");

  }

  @Test(priority = 2)
  public void coreLogic() {

    System.out.println("2- coreLogic");

  }

}

Execution order:

defaultPriority → initialize → coreLogic → cleanup

3. Running Prioritized Tests in TestNG with Selenium

In a Selenium test class you might want to open the browser, navigate, verify, then close—each as a separate test:

import org.testng.annotations.*;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumPriorityTest {

  private WebDriver driver;

  @BeforeClass
  public void setUp() {

    System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

    driver = new ChromeDriver();

  }

  @Test(priority = 0)
  public void openHomePage() {

    driver.get("https://example.com");

    System.out.println("Opened home page");

  }

  @Test(priority = 1)
  public void verifyTitle() {

    String title = driver.getTitle();

    assert title.contains("Example") : "Title mismatch";

    System.out.println("Verified title");

  }

  @Test(priority = 2)
  public void takeScreenshot() {

    // pseudo-code: ScreenshotUtil.capture(driver, "afterVerify");

    System.out.println("Captured screenshot");

  }

  @Test(priority = 3)
  public void closeBrowser() {

    driver.quit();

    System.out.println("Browser closed");

  }

}

Run via your usual Eclipse/IntelliJ run or testng.xml; TestNG will respect these priorities.

4. Same-Priority Tests in TestNG

If two or more methods share the same priority value (including the default 0), TestNG orders them alphabetically by method name:

@Test(priority = 1)
public void betaTest() { … }

@Test(priority = 1)
public void alphaTest() { … }

Execution:

alphaTest → betaTest

5. Analyzing Test Sequence with Test Priority in Selenium

To ensure your Selenium suite executes in the exact order you need:

1. Map out your scenario into discrete steps.

2. Assign priorities in ascending order of execution.

3. Verify by adding System.out.println or logging in each method.

Example run log:

0) defaultPriorityTest

1) openBrowser

2) navigateToHomePage

3) verifyTitle

4) logoutOrCleanup

If the sequence deviates, check for missing or duplicate priority values, or name collisions.

6. How to Skip a Test Case in TestNG

A. Using enabled=false

Quickly disable any test without removing it:

@Test(enabled = false)
public void flakyTest() {

  // won’t run or appear in reports

}

B. Using Groups/Exclusions in XML

In testng.xml:

<test name="MyTests">

  <groups>

    <run>

      <exclude name="slow"/>

    </run>

  </groups>

  <classes>

    <class name="com.example.tests.PriorityExample"/>

  </classes>

</test>

And mark tests with @Test(groups="slow").

7. Putting it all Together

By combining prioritization with grouping, dependencies, and conditional skipping, you can fully orchestrate complex Selenium suites that run exactly in the order—and only under the conditions—you specify.

testng framework
What are your Feelings

Share This Article :

  • Facebook
  • X
  • LinkedIn
 Hierarchy In TestNG AnnotationsTestNG Dependent
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105