2016-09-08 43 views
0

我在构造函数中有一个类@Factory(dataProvider = "dp")。 如何在数据提供者中获取该类?TestNG:如何获得由工厂从DataProvider创建的类

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数据提供者是在父类(或只是分离类)

回答

1

只需做到以下几点:

class Test { 

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

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

} 
+0

谢谢,朱利安,我会试试!这是记录在某处吗? –

+0

尚未:https://github.com/cbeust/testng/issues/1143但随时提出对文档的改进:D – juherr

+0

@SergeyIrisov根据你的问题,我的答案是否正确?如果是的话,你能接受吗? – juherr