--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/dda451fe-881a-48c0-841d-0516dd04bb94n%40googlegroups.com.
import static java.lang.annotation.ElementType.METHOD;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({METHOD})
public @interface ExcelDataSourceInfo {
String sheetName();
String filepath();
}
import java.lang.reflect.Method;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class GenericDataProviderTest {
@ExcelDataSourceInfo(sheetName = "sheet1", filepath = "src/test/resources/foo.xls")
@Test(dataProvider = "dp")
public void test1(String data) {
System.err.println("String received " + data);
}
@ExcelDataSourceInfo(sheetName = "sheet2", filepath = "src/test/resources/foo.xls")
@Test(dataProvider = "dp")
public void test2(int data) {
System.err.println("Integer received " + data);
}
@DataProvider(name = "dp")
public Object[][] genericDataProvider(Method method) {
ExcelDataSourceInfo info = method.getAnnotation(ExcelDataSourceInfo.class);
switch (info.sheetName().trim().toLowerCase()) {
case "sheet1":
return new Object[][] {
{"TestNG"},
{"JUnit5"},
{"JUnit4"}
};
case "sheet2" :
return new Object[][] {
{1},
{2},
{3}
};
default:
return new Object[][]{{}};
}
}
}
So my intent is as below, or something similar. Basically reuse same dataprovider used for different tests in same test script or in different test scripts. you were right, just provide the workbook name as a parameter from the @Test so dataprovider knows which workbook to pick data from. If that is not possible, how can the below be achieved using ITestContext, since it is not possible to pass an Itestcontext from withing the @Test?:Dataprovider will be in a separate class (say abc.java)@DataProvider (name = "name_of_dataprovider")public Object[][] dpMethod(String workbookName) {return new Object [][] { values}}From Test Script 1@Test (dataProvider = " dpMethod", dataproviderclass="abc", workbookName="workbook1.xlsx")public void myTest1 (String val) {System.out.println("Passed Parameter Is : " + val);}@Test (dataProvider = " dpMethod", dataproviderclass="abc", workbookName="workbook2.xlsx")public void myTest2 (String val) {System.out.println("Passed Parameter Is : " + val);}from Test Script 2@Test (dataProvider = " dpMethod", dataproviderclass="abc", workbookName="workbook3.xlsx")public void myTest3 (String val) {System.out.println("Passed Parameter Is : " + val);}
On Wednesday, March 9, 2022 at 10:58:41 PM UTC-8 Krishnan Mahadevan wrote:
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/006b562d-9b7c-4a9b-b9bf-529a02a711ebn%40googlegroups.com.