2013-02-17 31 views
0

我有一個實現了IInvokedMethodListener的TestNG偵聽器。在@BeforeMethod我需要設置一些測試情況下,這裏的例子:它是一種在配置階段在TestNG監聽器中獲取測試方法名稱的方法嗎?

public class ThucydidesInvokedMethodListener implements IInvokedMethodListener2 { 

    public void beforeInvocation(final IInvokedMethod method, final ITestResult testResult) { 

    boolean areBeforeMethods = method.getTestMethod().getTestClass().getBeforeTestMethods().length > 0; 
    if ((areBeforeMethods && method.getTestMethod().getTestClass().getBeforeTestMethods()[0] == method.getTestMethod()) || 
      !areBeforeMethods && method.isTestMethod()) { 

     final ThucydidesTestContext context = new ThucydidesTestContext(testResult); 
     testResult.setAttribute(contextKey(), context); 
     context.before(); 
    } 
} 

而且我需要後BeforeMethod在報告中使用這個測試的名字將被執行的測試名稱。這可能使用TestNG? 另外我試過IInvokedMethodListener2,它還有ITestContext,但它不提供測試名稱。

回答

2

使用監聽器來配置您的測試對我來說聽起來是錯誤的 - 這就是@ Before *註釋的用途。

我不知道如何用聽衆獲得所需的信息,但用@BeforeMethod很簡單:只需將一個類型爲java.reflect.Method的參數添加到方法簽名中,TestNG將注入當前的方法然後可以詢問它的名字和你想知道的其他一切。

所有的「神奇」的TestNG的註釋記錄在這裏:TestNG dependency injection

HTH

/延

1
import java.lang.reflect.Method; 

public class TestToGetMethodName 
{ 

    @BeforeMethod 
    public void handleTestMethodName(Method method) 
    { 
     String testName = method.getName(); 
    } 

} 
0

好了,有了IInvokedMethodListener的beforeInvocation方法讓你IInvokedMethod方法

method.getTestMethod.getMethodName()給你方法名稱。

相關問題