我创建了自定义TestNG TestListener并需要在jbehave中的@When块中设置Attribute。 我需要在我的自定义测试监听器类
中访问属性值@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 :(得分: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)
}
}
这会对你有用吗?