有条件地添加TestNG监听器?

huangapple go评论77阅读模式
英文:

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(&quot;on start&quot;);
    }

    @Override
    public void onExecutionFinish() {
        System.out.println(&quot;on finish&quot;);
    }
}
public class AlterSuiteListener implements IAlterSuiteListener {

    @Override
    public void alter(List&lt;XmlSuite&gt; suites) {
        for (XmlSuite suite: suites) {
            suite.addListener(String.valueOf(CustomListener.class));
        }
    }
}

testng.xml

&lt;suite name=&quot;all&quot; verbose=&quot;1&quot; parallel=&quot;tests&quot;&gt;
    &lt;listeners&gt;
        &lt;listener class-name=&quot;sandbox.AlterSuiteListener&quot; /&gt;
    &lt;/listeners&gt;
    &lt;test name=&quot;all&quot; parallel=&quot;methods&quot; thread-count=&quot;5&quot;&gt;
        &lt;packages&gt;
            &lt;package name=&quot;sandbox&quot; /&gt;
        &lt;/packages&gt;
    &lt;/test&gt;
&lt;/suite&gt;

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&lt;? extends ITestNGListener&gt; aClass) {
        if(condition()) {
            return new CustomListener();
        }
    }
}

And then registering listener via @Listeners({ConditionalListenerFactory.class})

huangapple
  • 本文由 发表于 2020年10月20日 00:07:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64431177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定