英文:
Conditionally add TestNG Listener?
问题
我想根据某些条件在编程方式下添加不同实现的TestNG监听器。
因为我无法找到通过@Listeners({})
注解提供工厂的方法,而且创建自己的TestNG运行器似乎不可行,所以我选择了IAlterSuiteListener
接口,根据文档:
> 允许您在运行时更改套件(或)套件XML文件中的测试标签
为了实现这一点,我创建并注册了一个简单的监听器:
public class CustomListener implements IExecutionListener {
@Override
public void onExecutionStart() {
System.out.println("on start");
}
@Override
public void onExecutionFinish() {
System.out.println("on finish");
}
}
public class AlterSuiteListener implements IAlterSuiteListener {
@Override
public void alter(List<XmlSuite> suites) {
for (XmlSuite suite: suites) {
suite.addListener(String.valueOf(CustomListener.class));
}
}
}
testng.xml
<suite name="all" verbose="1" parallel="tests">
<listeners>
<listener class-name="sandbox.AlterSuiteListener" />
</listeners>
<test name="all" parallel="methods" thread-count="5">
<packages>
<package name="sandbox" />
</packages>
</test>
</suite>
为了确保AlterSuiteListener
起作用,我尝试简单地更改了一些套件配置(设置测试超时),它确实起作用。然而,添加监听器没有任何效果。
提前谢谢。
英文:
I want to programmatically add different implementations of TestNG Listener based on some conditions.
As I wasn't able to find any means to provide it with some sort of factory via @Listeners({})
annotation and creating my own TestNG runner does not seem feasible, I've settled on the IAlterSuiteListener
interface, which, according to documentation:
> allows you to alter a suite (or) a test tag in your suite XML file at
> runtime
To achieve this I've created and registered a simple listener:
public class CustomListener implements IExecutionListener {
@Override
public void onExecutionStart() {
System.out.println("on start");
}
@Override
public void onExecutionFinish() {
System.out.println("on finish");
}
}
public class AlterSuiteListener implements IAlterSuiteListener {
@Override
public void alter(List<XmlSuite> suites) {
for (XmlSuite suite: suites) {
suite.addListener(String.valueOf(CustomListener.class));
}
}
}
testng.xml
<suite name="all" verbose="1" parallel="tests">
<listeners>
<listener class-name="sandbox.AlterSuiteListener" />
</listeners>
<test name="all" parallel="methods" thread-count="5">
<packages>
<package name="sandbox" />
</packages>
</test>
</suite>
To make sure AlterSuiteListener
works I've tried simply changing some suite configuration(setting test timeout) and it does work. Adding listener, however, does not take any effect.
Thank you in advance.
答案1
得分: 2
我已经找到了使用 ITestNGListenerFactory 的解决方案:
public class ConditionalListenerFactory implements ITestNGListenerFactory, ITestNGListener {
public ConditionalListenerFactory() {
}
public ITestNGListener createListener(Class<? extends ITestNGListener> aClass) {
if (condition()) {
return new CustomListener();
}
}
}
然后通过 @Listeners({ConditionalListenerFactory.class})
注册监听器。
英文:
I've found a solution using ITestNGListenerFactory:
public class ConditionalListenerFactory implements ITestNGListenerFactory, ITestNGListener {
public ConditionalListenerFactory() {
}
public ITestNGListener createListener(Class<? extends ITestNGListener> aClass) {
if(condition()) {
return new CustomListener();
}
}
}
And then registering listener via @Listeners({ConditionalListenerFactory.class})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论