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.