TestNG for Selenium: Annotations, Assertions and Data
TestNG runs your tests, decides the order, and reports what passed and failed. JUnit 5 does the same job. Most Selenium + Java teams use TestNG.
Annotations
| Annotation | Runs | Typical use |
|---|---|---|
@BeforeSuite | Once before everything | Read config, start a report |
@BeforeClass | Once before a test class | Create test data |
@BeforeMethod | Before every test | Start the browser |
@Test | The test itself | Steps and assertions |
@AfterMethod | After every test | Screenshot on failure, quit the browser |
@AfterClass / @AfterSuite | At the end | Clean up data, finish the report |
Assertions
Assert.assertEquals(actual, expected);
Assert.assertTrue(button.isEnabled(), "Log in button should be enabled");
Assert.assertFalse(error.isDisplayed());
A hard assertion stops the test at the first failure. A soft assertion collects all failures and reports them at the end:
SoftAssert soft = new SoftAssert();
soft.assertEquals(nameText, "Demo Tester");
soft.assertEquals(emailText, "demo@testqualityhub.com");
soft.assertAll(); // do not forget this line
Priorities, groups and disabling
@Test(groups = {"smoke"}, description = "Valid user can log in")
public void validLogin() { ... }
@Test(groups = {"regression"}, dependsOnMethods = "validLogin")
public void logout() { ... }
@Test(enabled = false) // skipped until bug 123 is fixed
public void remembersUser() { ... }
Try not to make tests depend on each other. A test that needs another test to pass first is hard to run alone.
Data-driven tests with a DataProvider
@DataProvider(name = "badLogins")
public Object[][] badLogins() {
return new Object[][] {
{"", "Demo@1234", "Email is required"},
{"demo@testqualityhub.com", "", "Password is required"},
};
}
@Test(dataProvider = "badLogins")
public void showsFieldError(String email, String password, String message) {
loginPage.open();
loginPage.logIn(email, password);
Assert.assertTrue(loginPage.fieldErrors().contains(message));
}
One test method, many rows of data. This is how you cover equivalence classes and boundary values without copying code.
testng.xml
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Practice suite" parallel="methods" thread-count="2">
<test name="Smoke">
<groups><run><include name="smoke"/></run></groups>
<classes>
<class name="tests.LoginTest"/>
</classes>
</test>
</suite>
Run a suite from the terminal with mvn test -DsuiteXmlFile=testng.xml after adding the Surefire plugin to your pom.xml.
Practice: turn the boundary values from the boundary value lesson into a DataProvider for the practice register page.
All 9 lessons in Selenium with Java
- What Is Selenium WebDriver?
- Set Up Java, Maven and Selenium
- Write Your First Selenium Test
- Selenium Locators: ID, CSS, XPath and More
- Clicks, Typing, Dropdowns, Alerts, Frames and Windows
- Waits in Selenium: Implicit, Explicit and Fluent
- TestNG for Selenium: Annotations, Assertions and Data
- Page Object Model in Selenium Java
- Selenium Framework Structure, Reports and CI