TestNG Annotations
Common Annotations
@Test— marks a method as a test case.@BeforeMethod/@AfterMethod— run before/after each test method.@BeforeClass/@AfterClass— run once before/after all tests in a class.@BeforeSuite/@AfterSuite— run once before/after the entire suite.@DataProvider— supplies multiple sets of data to a test method for data-driven testing.
Why Annotations Matter
Annotations let testers precisely control the setup and teardown lifecycle around each test, avoiding repeated boilerplate code.
The Order in Which Annotations Run
When TestNG runs a suite, the configuration methods wrap the tests like layers of an onion. For a suite with one test tag and one class containing two tests, the order is:
@BeforeSuite@BeforeTest(for each<test>tag in testng.xml)@BeforeClass@BeforeMethod, then the first@Test, then@AfterMethod@BeforeMethod, then the second@Test, then@AfterMethod@AfterClass@AfterTest@AfterSuite
There are also @BeforeGroups and @AfterGroups, which run around tests that belong to a named group.
Worked Example: A Selenium Test Class
public class LoginTests {
WebDriver driver;
@BeforeClass
public void setUp() {
driver = new ChromeDriver();
}
@BeforeMethod
public void openLoginPage() {
driver.get("https://example.com/login");
}
@Test(priority = 1, groups = "smoke")
public void validLogin() {
// enter valid credentials and assert the dashboard loads
}
@Test(priority = 2, dataProvider = "badUsers")
public void invalidLogin(String user, String pass) {
// enter bad credentials and assert an error message
}
@DataProvider(name = "badUsers")
public Object[][] badUsers() {
return new Object[][] {
{"wrong@example.com", "Secret123"},
{"tester@example.com", "wrongpass"},
{"", ""}
};
}
@AfterMethod
public void captureOnFailure(ITestResult result) {
if (result.getStatus() == ITestResult.FAILURE) {
// take a screenshot here
}
}
@AfterClass
public void tearDown() {
driver.quit();
}
}
The browser opens once per class, the login page reloads before each test, and the data provider makes invalidLogin run three times with different inputs.
The ITestResult parameter in @AfterMethod is injected by TestNG automatically, so the cleanup method can see whether the test that just ran passed, failed or was skipped. Configuration methods can also receive other objects, such as ITestContext for suite-level details or Method to know which test is about to run. To run only the smoke group, list it under the include tag of the groups section in testng.xml.
Useful Attributes of @Test
| Attribute | What It Does |
|---|---|
priority | Lower numbers run first when no dependency decides the order |
groups | Tags tests, for example "smoke" or "regression", so testng.xml can include or exclude them |
dependsOnMethods | Skips the test if a named test failed |
enabled | Set to false to switch a test off temporarily |
timeOut | Fails the test if it runs longer than the given milliseconds |
expectedExceptions | Passes only if the listed exception is thrown |
invocationCount | Runs the same test several times |
Common Annotation Mistakes
- Heavy work in
@BeforeMethod. Starting a browser before every single test makes suites slow; use@BeforeClasswhen tests can share it safely. - Relying on priority for data flow. If test B needs data from test A, prefer
dependsOnMethodsor, better, make each test create its own data. - Shared driver in parallel runs. A single field shared across threads causes random failures. Use a ThreadLocal driver when running tests in parallel.
- Mixing JUnit and TestNG imports. An
@Testfrom the wrong package is silently ignored by the TestNG runner. - Forgetting
alwaysRun = trueon cleanup methods that must run even when group filters are applied.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.