2016-09-08 38 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