我在构造函数中有一个@Factory(dataProvider = "dp")类。
如何在数据提供程序中获取该类?

class Test {

   @Factory(dataProvider = "dp")
   public Test(int i) {
      //... some code
   }

   @DataProvider
   public static Object[][] dp(ITestContext context, Method method) {
       // need to get currently created class by factory
       // method is null here
       // not found any way to obtain this class from test context
   }

}


在此示例中,我可以使用硬编码的类名,但实际上,worl数据提供程序位于父类(或只是单独的类)中

最佳答案

只需执行以下操作:

class Test {

   @Factory(dataProvider = "dp")
   public Test(int i) {
      //... some code
   }

   @DataProvider
   public static Object[][] dp(ConstructorOrMethod com) {
       Class<?> testClass = com.getConstructor().getDeclaringClass();
   }

}

关于java - TestNG:如何从DataProvider获取将要由Factory创建的类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39391569/

10-10 12:57