TestNG Framework
Dashboard
Topic 6/13
Home โ€บ Testing โ€บ TestNG Framework

โœ… TestNG Framework

TestNG annotations, assertions, DataProviders, testng.xml, and parallel execution.

What is TestNG?

TestNG (Test Next Generation) is a testing framework inspired by JUnit and NUnit. It introduces powerful features like annotations, parameterization, parallel execution, and HTML reporting.

Key TestNG Annotations

Execution order of TestNG setup and teardown annotations.

Annotation Execution Frequency
@BeforeSuite / @AfterSuite Runs once before / after all tests in suite.
@BeforeClass / @AfterClass Runs once before / after first/last method of class.
@BeforeMethod / @AfterMethod Runs before / after EVERY @Test method.
@Test Marks a method as part of the test suite.
@DataProvider Feeds data rows into test methods for parameterization.

Data Driven Testing with @DataProvider

Pass dynamic test dataset arrays into automated tests.

LoginDataTest.java Java
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class LoginDataTest {

  @DataProvider(name = "loginCredentials")
  public Object[][] getData() {
    return new Object[][] {
      { "admin", "pass123", true },
      { "invalidUser", "wrongPass", false }
    };
  }

  @Test(dataProvider = "loginCredentials")
  public void testLogin(String user, String pass, boolean expected) {
    System.out.println("Testing login for: " + user);
    Assert.assertTrue(true);
  }
}

Suite Configuration with testng.xml

Configure test execution suites, groups, and parallel thread execution.

testng.xml XML
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Automation Test Suite" parallel="methods" thread-count="4">
  <test name="Regression Tests">
    <classes>
      <class name="com.example.tests.LoginDataTest" />
    </classes>
  </test>
</suite>