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

Click on the Image to view it in full screen

DataDriven-TestNG

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

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

TestNG-DataProvider

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.

Test

Data provider

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

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

Using the Data provider present in same class in Test

@Test(dataProvider="getData")
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:

@DataProvider(name="scenarioData")
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
workbook.close();

System.out.println(obj.toString());
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);
		
}

parallel

  • 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)

indices

- 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})

name

  • 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”)

ITestContext

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

Method

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

TestNG-XML

Description:

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.

Example:

testng.xml-


com.test.className

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

XMind: ZEN – Trial Version

Reference:
https://examples.javacodegeeks.com/enterprise-java/testng/testng-dataprovider-example/

Leave a Reply