TestNG:如何从DataProvider获取即将由Factory创建的类



我有一个类与@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
   }
}

在这个例子中,我可以使用硬编码的类名,但在现实世界中,数据提供程序是在父类(或只是分离类)

只需做以下操作:

class Test {
   @Factory(dataProvider = "dp")
   public Test(int i) {
      //... some code
   }
   @DataProvider
   public static Object[][] dp(ConstructorOrMethod com) {
       Class<?> testClass = com.getConstructor().getDeclaringClass();
   }
}

相关内容

最新更新