TestNG Framework for Selenium

Harry · 21 Sep 2026 · 1 views
Log in to track your progress and mark lessons complete.

Introduction to TestNG

TestNG organises tests with annotations, groups, parallel runs and rich HTML reports. It is the runner behind most Java Selenium frameworks.

Annotations

@BeforeSuite @BeforeClass @BeforeMethod
@Test(priority = 1, groups = "smoke")
@AfterMethod @AfterClass @AfterSuite

Assertions

Assert.assertEquals(actual, expected); Assert.assertTrue(isLoggedIn). A failed assert fails the test and logs expected vs actual.

Groups, DataProvider, Parameters

@Test(groups = {"smoke", "regression"})
@DataProvider(name = "logins")
public Object[][] data() {
  return new Object[][] {
    {"user@test.com", "Pass@123", true},
    {"bad@test.com", "wrong", false} };
}
@Test(dataProvider = "logins")
public void login(String u, String p, boolean ok) { ... }

Reports and Parallel Testing

TestNG emits test-output HTML reports; plug ExtentReports for dashboards with screenshots. Run suites in parallel with testng.xml parallel=methods thread-count=4 to cut suite time.

Selenium + TestNG Project Layout

src/test/java/pages/LoginPage.java
src/test/java/tests/LoginTest.java
src/test/resources/testng.xml + testdata.csv

TestNG annotation execution order from BeforeSuite to AfterMethod

Key Points

  • Annotations control order; groups control scope.
  • DataProvider turns one test into fifty.
  • Parallel runs plus CI equal fast feedback.
Share this post:

Comments (0)

Please login or register to comment.

Create a free account to keep reading

You've enjoyed a free tutorial! Register (it's free) to unlock every tutorial, track your progress and save code.

or sign in with your account

Already have an account? Log in