Search code examples
testngdataprovider

different excel sheets for same dataprovider


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


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.