TestNG: Data driven testing using Data Provider and testng.xml

Click on the Image to view it in full screen


TestNG supports two different ways of injecting parameter values.
Parameterization through testng.xml

There is also the factory way where we create multiple instances of the test class with different constructor data.


If you want to provide the test data, the DataProvider way, then we need to declare a method that returns the data set in the form of two dimensional object array Object[][]. The first array represents a data set whereas the second array contains the parameter values.

The DataProvider method can be in the same test class or one of its super classes. It is also possible to provide DataProvider in another class but then the method has to be static.


Data provider

//Data provider without name: It will take the method name
public Object[][] getData() {
return new Object[][]{{5, "five"}, {6, "six"}};

//Data provider with name
public Object[][] login() {
return new Object[][]{{5, "five"}, {6, "six"}};

Using the Data provider present in same class in Test

public void instanceDbProvider(int p1, String p2) {
System.out.println("Instance DataProvider Example: Data(" + p1 + ", " + p2 + ")");

Using the data provider from other class

@Test(dataProvider="loginData", dataProviderClass = Test4DataProvider.class)

Using the data based on method name:

public static Object[][] getScenarioData(Method method) {
String testCase = method.getName();
if ("scenario1".equals(testCase)) {
return new Object[][]{{"Scenario1 data"}};

Data provider using Excel:

Excel reader code snippet:

public static Object[][] readExcelBySheetName(String sheetName, String filePath)throws IOException, InvalidFormatException {

//  Constructing XSSFWorkbook object using String filepath
XSSFWorkbook workbook = new XSSFWorkbook(filePath);

//  Getting the sheet by Name
XSSFSheet sheet = workbook.getSheet(sheetName);

	int totalRows = sheet.getLastRowNum();
	int totalColums = sheet.getRow(0).getPhysicalNumberOfCells();
* Use opening Single quote ( ' ) to pass empty string in excel
// Read data from excel and store the same in the Object Array.
Object obj[][] = new Object[totalRows][totalColums];
for (int i = 0; i < totalRows; i++) {
	obj[i][0] = sheet.getRow(i + 1).getCell(0).toString();
	obj[i][1] = sheet.getRow(i + 1).getCell(1).toString();

// Closing Workbook connection

return obj;
@DataProvider(name = "loginCredentials")
public Object[][] getLoginData() throws IOException, InvalidFormatException {
	String filePath = "./src/test/resources/ExcelData/TestData.xlsx";
	Object obj[][] = readExcelBySheetName("Sheet1",filePath);
	return obj;
@Test(dataProvider = "loginCredentials", dataProviderClass = UtilsTestDataProvider.class)
public void tesDataProv(String email, String password){
	System.out.println("**"+email + "**"+password);


  • This parameter tells TestNG to run tests in parallel. So the input to test methods can be injected in random order. By default inputs are injected in the order and tests are executed sequentially.
  • @DataProvider(parallel = true)


- This is a very important property, this allows us to specify the indexes we want to use for our testing purpose. This is helpful when our data provider method may have a large number of values and we want to run our test cases for only a few of them. Its default value is “all” so tests will run for all the inputs generated by the data provider method.
- @DataProvider( indices= {1,3,5,7,9})


  • This is used to specify the data provider method name, default value is the function name. We can use it to give a proper name to our Data Provider method.

But this is optional. If one hasn’t provided the name, name of the method will be used to refer to it.

  • @DataProvider(name=”fooDP”)


  • @DataProvider(name=”TestType”)
    public static Object[][] getTestTypeData(ITestContext context) {
    String testName = context.getName();
    if (“IntegrationLevel”.equals(testName)) {
    return new Object[][]{{“Integration test data”}};


  • @DataProvider(name=”scenarioData”)
    public static Object[][] getScenarioData(Method method) {
    String testCase = method.getName();
    if (“scenario1”.equals(testCase)) {
    return new Object[][]{{“Scenario1 data”}};



Parameterization through testng.xml is one of the methods of injecting parameter values. This method is useful if the data set is limited to just a few rows and the values are of simple types like String, int etc. The parameters are declared in testng.xml, where the name attribute defines name of the parameter and value attribute defines the parameter value.




@Parameters({“param1”, “param2”})
public void paramTest(int p1, String p2) {
System.out.println(“Parameter Example: Data(” + p1 + “, ” + p2 + “)”);

XMind: ZEN – Trial Version


Leave a Reply