2016-12-01 72 views
0

我已經創建了自定義TestNG TestListener,並且需要在jbehave中設置@When塊中的Attribute。 我需要在我的自定義測試監聽器類如何在Jbehave中調用TestNG自定義測試監聽器

@Listeners(TestReportListener.class) 
public class MyJBehaveScenario { 

@Given("the balance is <balance>") 
public void createAccount(@Named("balance") int balance) { 
    account = new Account(balance); 
} 

@When("the requests <request>") 
public void requestMoney(@Named("request") int amount) { 
    Reporter.getCurrentTestResult().setAttribute("ABC", "XYZ"); 
} 

@Then("the card should be returned") 
public void cardShouldBeReturned() { 
    System.out.println("Completed") 
} 

}

回答

0

也許你可以嘗試做這樣的事情與訪問屬性值。

首先讓你的監聽者表現得像是一個單身人士。

public class TestReportListener implements IInvokedMethodListener { 
    private static TestReportListener instance; 
    private String myAttribute; 

    private static void setInstance(TestReportListener ins) { 
     instance = ins; 
    } 
    public static TestReportListener getInstance() { 
     return instance; 
    } 

    public TestReportListener() { 
     //Make sure you are using TestNG 6.10 because this version of TestNG ensures that 
     //TestNG does not register duplicate listener instances into it. 
     setInstance(this); 
    } 

    public String getMyAttribute() { 
     return myAttribute; 
    } 

    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) { 
     this.myAttribute=testResult.getName(); 
     //do some work 
    } 

    public void afterInvocation(IInvokedMethod method, ITestResult testResult) { 
     //do some more work 
    } 
} 

現在修改後的測試類可能類似於如下:

@Listeners(TestReportListener.class) 
public class MyJBehaveScenario { 
    @Given("the balance is <balance>") 
    public void createAccount(@Named("balance") int balance) { 
     account = new Account(balance); 
    } 

    @When("the requests <request>") 
    public void requestMoney(@Named("request") int amount) { 
     Reporter.getCurrentTestResult().setAttribute("ABC", "XYZ"); 
    } 

    @Then("the card should be returned") 
    public void cardShouldBeReturned() { 
     System.out.println("Completed"); 
     //Now lets try accessing the attribute from our listener 
     String attribute = TestReportListener.getInstance().getMyAttribute(); 
     System.out.println("Value of the attribute is " : attribute) 
    } 
} 

會爲你工作?

+0

我試過但不適合我。 是否有任何可能獲得測試結果上下文jbehave喜歡在TestNG中的ITestResult –

+0

@AshanJayasundara您需要告訴我們您究竟想要做什麼。 –

+0

我已經通過分析日誌文件開發了測試報告功能,它是使用TestNG [獲取測試結果,執行時間,...]構建的。 現在我需要將相同的機制應用於jbehave測試報告 –

相關問題