Question

Is it possible to use different excel sheets for different test methods sharing the same dataprovider in testng

Was it helpful?

Solution

If the method with the @DataProvider annotation accepts a java.lang.reflect.Method as it's first argument, TestNG will pass the currently executing test method as the parameter. For example

@Test(dataprovider="dp1")
public void test1(String str){
//test here
}

@DataProvider(name="dp1")
public Object [][](Method testName){
// testName will be the calling method
// testName.getName(); == "test1"
return new Object[][]{new Object[]{"Cedric"}};
}

Using this, you can create a sheet for each test method and name it the same as the method name. Then your dataprovider can lookup your sheet, and return however many parameters are desired.

OTHER TIPS

Sure, just have your data provider read all the Excel spreadsheets, combine them into one Object[][] and return that object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top