Can I order my TestNG listeners?

我的未来我决定 提交于 2019-12-25 17:01:52

问题


I have a situation where I need to enable/disable tests(using AnnotationTransformer listener) based on the parameter coming from my suite files(using ISuiteListener listener). However, when I try to read the parameters in my suite file before calling AnnotationTransformer, I am not able to do so.

This does not help:

<listeners preserve-order="true">
        <listener class name="automation.test.SentinelSuiteListener" />
        <listener class-name="automation.test.AnnotationTransformer" />
</listeners>

Also, when I try to implement both these interfaces together, the method for AnnoataionTransformer i.e. transform goes before the onStart() method:

public class AnnotationTransformer implements IAnnotationTransformer, ISuiteListener {

    String currentRunModeInSuite;

    @Override
    public void onStart(ISuite suite) {
        currentRunModeInSuite = suite.getParameter("currentRunMode");
    }

    @Override
    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
        System.out.println("***Test case under review : " + testMethod.getName());
        for(int i=0; i<annotation.getGroups().length;i++){
            System.out.println(annotation.getGroups()[i]);
        }
        System.out.println(currentRunModeInSuite);
    }  


    @Override
    public void onFinish(ISuite suite) {
        // TODO Auto-generated method stub
    }
} 

回答1:


The behavior you seen is the expected one because TestNG works step by step.

First, it looks for tests and reads their datas. You can be triggered if you want to change them and it's the goal of IAnnotationTransformer.

Then, TestNG runs suite/tests/classes/methods and you can be triggered by that with ISuiteListener or else.

What you need is IMethodInstance which is called during the run: http://testng.org/doc/documentation-main.html#methodinterceptors

Fyi, the <listeners> node doesn't accept preserve-order attribute. See the documentation: http://testng.org/testng-1.0.dtd.php



来源:https://stackoverflow.com/questions/36009741/can-i-order-my-testng-listeners

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!