Core Annotations
Lifecycle annotations help define setup and cleanup at the right scope.
@BeforeSuite @BeforeClass @BeforeMethod @Test @AfterMethod @AfterClass
A compact TestNG reference for building maintainable Selenium suites with the right annotations, data flow, and execution controls.
Lifecycle annotations help define setup and cleanup at the right scope.
@BeforeSuite @BeforeClass @BeforeMethod @Test @AfterMethod @AfterClass
Keep tests focused and independent.
@Test
public void validLoginShouldWork() {
Assert.assertTrue(true);
}
Use these for organization, not as a replacement for strong test independence.
@Test(priority = 1, groups = {"smoke", "login"})
public void loginTest() {}
Use cautiously. Strong suites usually avoid chaining tests together.
@Test
public void createAccount() {}
@Test(dependsOnMethods = "createAccount")
public void verifyAccount() {}
TestNG assertions are direct and useful for core validation work.
Assert.assertEquals(actual, expected); Assert.assertTrue(condition); Assert.assertNotNull(object);
Soft assertions let multiple checks run before failing together.
SoftAssert soft = new SoftAssert();
soft.assertTrue(true);
soft.assertEquals("A", "A");
soft.assertAll();
Use DataProviders for simple data-driven test coverage.
@DataProvider
public Object[][] loginData() {
return new Object[][] {
{"user1", "pass1"},
{"user2", "pass2"}
};
}
XML suites help organize execution, parallelization, and parameters.
<suite name="Regression">
<test name="Login Tests">
<classes>
<class name="tests.LoginTest"/>
</classes>
</test>
</suite>
Use listeners for reporting and retries for controlled flaky behavior only.
public class Retry implements IRetryAnalyzer {
public boolean retry(ITestResult result) {
return false;
}
}
Parallel runs speed up suites, but only if setup and data are isolated.
<suite parallel="tests" thread-count="3">