TestNG Documentation
TestNG Documentation
TestNG (Test Next Generation) is a testing framework inspired by JUnit and NUnit, but with
enhanced features. It simplifies testing in Selenium-Java projects with annotations, grouping,
parameterization, and parallel execution.
1. Setup TestNG
Add to Maven pom.xml:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
</dependency>
2. TestNG Annotations
1. Suite-Level Annotations
2. Test-Level Annotations
4. Method-Level Annotations
Execution Order
@Test(priority = 2)
public void loginTest() { ... }
4. Grouping Tests
Group tests using groups parameter:
@Test(groups = "smoke")
public void homePageTest() { ... }
@Test(groups = "regression")
public void checkoutTest() { ... }
<groups>
<run>
<include name="smoke"/>
</run>
</groups>
5. Parameters
XML Parameters:
@Test
@Parameters({"url", "username"})
public void loginTest(String url, String username) {
driver.get(url);
// Use username
}
In testng.xml:
<parameter name="url" value="https://example.com"/>
<parameter name="username" value="admin"/>
DataProvider:
@DataProvider(name = "loginData")
public Object[][] provideData() {
return new Object[][] {
{"user1", "pass1"},
{"user2", "pass2"}
};
}
@Test(dataProvider = "loginData")
public void dataDrivenTest(String user, String pass) {
// Test with user/pass
}
6. Dependencies
Run tests conditionally:
@Test
public void setup() { ... }
@Test(dependsOnMethods = "setup")
public void mainTest() { ... } // Runs only if setup() passes
7. Assertions
Use TestNG assertions:
@Test
public void assertionTest() {
assertEquals(actual, expected);
assertTrue(condition);
assertNotNull(object);
assertFalse(condition);
assertEquals(actual, expected, "Custom error message");
}
Soft Assertions
import org.testng.asserts.SoftAssert;
@Test
public void softAssertExample() {
SoftAssert softAssert = new SoftAssert();
Parallel options:
9. Listeners
Implement listeners for logging/reporting:
public class CustomListener implements ITestListener {
@Override
public void onTestSuccess(ITestResult result) {
System.out.println("Test passed: " + result.getName());
}
@Override
public void onTestFailure(ITestResult result) {
// Capture screenshot for Selenium tests
System.out.println("Test failed: " + result.getName());
}
@Override
public void onStart(ITestContext context) {
System.out.println("Starting test: " + context.getName());
}
@Override
public void onFinish(ITestContext context) {
System.out.println("Finishing test: " + context.getName());
}
}
Add to testng.xml:
<listeners>
<listener class-name="com.example.CustomListener"/>
</listeners>
@Listeners(com.example.CustomListener.class)
public class MyTestClass {
// Test methods
}
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>5.0.9</version>
</dependency>
@Override
public void onStart(ITestContext context) {
String reportPath = "reports/extent-report.html";
extent = new ExtentReports();
extent.attachReporter(new ExtentHtmlReporter(reportPath));
}
@Override
public void onTestStart(ITestResult result) {
String methodName = result.getMethod().getMethodName();
ExtentTest testReport = extent.createTest(methodName);
test.set(testReport);
}
@Override
public void onTestSuccess(ITestResult result) {
test.get().pass("Test passed");
}
@Override
public void onTestFailure(ITestResult result) {
test.get().fail(result.getThrowable());
// Add screenshot capture code here for Selenium tests
}
@Override
public void onFinish(ITestContext context) {
extent.flush();
}
}
12. Timeouts
Set timeouts for tests:
@Test
public void testParameter() {
System.out.println("Parameter: " + param);
}
}
<test name="SmokeTests">
<parameter name="browser" value="firefox"/>
<classes>
<class name="com.example.SmokeTests"/>
</classes>
</test>
</suite>
@Test(configFailurePolicy = ConfigurationFailurePolicy.CONTINUE)
public void testMethod() {
// This will run even if @BeforeMethod fails
}
@Override
public boolean retry(ITestResult result) {
if (!result.isSuccess()) {
if (count < MAX_RETRY) {
count++;
return true; // Retry the test
}
}
return false; // No more retries
}
}
//Apply to a test
@Test(retryAnalyzer = RetryAnalyzer.class)
public void flakyTest() {
// This test will retry up to MAX_RETRY times if it fails
}
@Test
public void validLogin() {
driver.get("https://example.com/login");
driver.findElement(By.id("username")).sendKeys("validUser");
driver.findElement(By.id("password")).sendKeys("validPass");
driver.findElement(By.id("loginButton")).click();
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, "Dashboard", "Login failed");
}
@AfterClass
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}