简体   繁体   中英

How to call a parameterized method (using dataprovider) from a different class?

I am facing an issue wherein I have created a parameterized method that uses dataprovider. Both DataProvider and method and created in same class. Now, I want to call this method from another class but it requires parameters to be passed which I cannot since they are being read from dataProvider.

I have tried declaring the dataProvider in different class too but that doesn't work. Please suggest some workaround for it.

NOTE: I have a restriction that I cannot use TestNG.xml to implement this scenario.

Please find the code below:

DataProvider:

@DataProvider(name = "TestSuite")
    public Object[][] dataSheetTraverser() {
        String SheetName = "ProgLang";
        datatable = new Xls_Reader(TestDataSheetPath_ProgLang);
        int rowcount = datatable.getRowCount(SheetName);
        Object result[][] = new Object[rowcount - 1][3];
        for (int i = 2; i < rowcount + 1; i++) {
            result[i - 2][0] = SheetName;
            result[i - 2][1] = i;
            result[i - 2][2] = datatable.getCellData(SheetName, "caseType", i);

        }
        return result;
    }

Test Method:

@Test(dataProvider="TestSuite_ProgLang",priority =2)
    public void TC_Verify_EditProgLang(String SheetName,int i, String caseType)
    {

        String test1= datatable.getCellData(SheetName, "Skills", i);
        String test2= datatable.getCellData(SheetName, "Version", i);
        String test3= datatable.getCellData(SheetName, "LastUsed", i);
        String test4= datatable.getCellData(SheetName, "ExperienceYr", i);
        String ExperienceMn = datatable.getCellData(SheetName, "ExperienceMn", i);

        proglang.FillForm_ProgLang(Skills, Version, LastUsed, ExperienceYr, ExperienceMn);

    }

I want to call the above function TC_Verify_EditProgLang from another class. Please suggest.

You can use the dataProviderClass attribute for calling from other class
in @Test and The Provider method Must be static :

public class StaticProvider {
      @DataProvider(name = "create")
      public static Object[][] createData() {
        return new Object[][] {
          new Object[] { new Integer(42) }
        };
      }
    }
    //different Class
    public class MyTest {
      @Test(dataProvider = "create", dataProviderClass = StaticProvider.class)
      public void test(Integer n) {
        // ...
      }
    }

please check the Documentation : dataProviders

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM