</>

Technology

TestNG

Difficulty

Intermediate

Interview Question

How can we create a data driven framework using TestNG?

Use @DataProvider annotation to supply multiple data sets to a single test method, enabling data-driven testing in TestNG.

Answer

Data-Driven Framework Using TestNG @DataProvider

TestNG''s @DataProvider lets you run the same test method with different data sets — the foundation of data-driven testing.

How it works:

  1. Create a method annotated with @DataProvider that returns Object[][] (2D array)
  2. Reference the provider in your @Test method via dataProvider="name"
  3. TestNG automatically runs the test once per data row

Basic example:

Java
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataDrivenExample {

    @DataProvider(name = "loginData")
    public Object[][] getData() {
        return new Object[][] {
            {"user1", "pass1"},
            {"user2", "pass2"},
            {"user3", "pass3"}
        };
    }

    @Test(dataProvider = "loginData")
    public void loginTest(String username, String password) {
        System.out.println("Testing with: " + username + " / " + password);
        // This test runs 3 times — once per row
    }
}

Full Selenium data-driven login test:

Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.*;

public class DataProviderExample {

    WebDriver driver;

    @BeforeClass
    void setup() {
        System.setProperty("webdriver.chrome.driver", "C://Drivers/chromedriver.exe");
        driver = new ChromeDriver();
    }

    @Test(dataProvider = "users")
    void loginTest(String uname, String pwd) {
        driver.get("http://newtours.demoaut.com/");
        driver.findElement(By.name("userName")).sendKeys(uname);
        driver.findElement(By.name("password")).sendKeys(pwd);
        driver.findElement(By.name("login")).click();
        Assert.assertEquals(driver.getTitle(), "Find a Flight: Mercury Tours:");
    }

    @DataProvider(name = "users")
    String[][] logindata() {
        String data[][] = new String[][] {
            {"mercury", "mercury"},
            {"asasa",   "mercury1"},
            {"mercury2","mercury2"}
        };
        return data;
    }

    @AfterClass
    void closeBrowser() {
        driver.quit();
    }
}

Reading data from Excel (with Apache POI):

Java
@DataProvider(name = "excelData")
public Object[][] getExcelData() throws Exception {
    FileInputStream fis = new FileInputStream("testdata.xlsx");
    XSSFWorkbook wb = new XSSFWorkbook(fis);
    XSSFSheet sheet = wb.getSheet("Sheet1");
    int rows = sheet.getLastRowNum();
    Object[][] data = new Object[rows][2];
    for (int i = 1; i <= rows; i++) {
        data[i-1][0] = sheet.getRow(i).getCell(0).getStringCellValue();
        data[i-1][1] = sheet.getRow(i).getCell(1).getStringCellValue();
    }
    wb.close();
    return data;
}

Key points:

  • @DataProvider method must return Object[][]
  • Test runs once per row in the 2D array
  • Use parallel = true in @DataProvider to run iterations in parallel
  • External data sources (Excel, CSV, DB) can feed the provider

Follow AutomateQA

Related Topics