views:

37

answers:

2

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

A: 

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

Cedric Beust
But I want to take the sheet name while running the test for example my data provider is:-@DataProvider(name = "DP") public Object[][] createData1() throws Exception { Object[][] retObjArr=datacreate.getTableArray(path,tablename,sheetname); return(retObjArr); }and I have two test methods@Test(dataProvider = "DP") public void testAddUser(String username, String location){//} and public void testDeleteUser(int userid, String username){//}So I want data to be taken different excel sheets while running the test.Please let me know how can i achieve this.
TestUser
Have your data provider return an array of { three parameters }, each of your test method takes three parameters and it only uses the two it needs.
Cedric Beust
Still not able to do it.I guess it wants the sheet name before it invokes the test method and I m providing sheet name inside the testmethod.Is there any way I can write the Data Provider like:- public Object[][]createData1(String sheetname) throws Exception { Object[][]retObjArr=datacreate.getTableArray(path,tablename,sheetname); return(retObjArr); }and in test method something like this:-@Test(dataProvider = "DP", "Sheet1") public void testAddUser(int userid, String username)
TestUser
It's getting hard to read the code here, do you mind posting your problem to the testng-users list? It will be easier.
Cedric Beust
I have already done thatwill add the code to it
TestUser
A: 

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.

pnewhook
I got this working.
TestUser