我可以订购我的 TestNG 监听器吗? [英] Can I order my TestNG listeners?
问题描述
我有一种情况,我需要根据来自我的套件文件的参数(使用 ISuiteListener 侦听器)启用/禁用测试(使用 AnnotationTransformer 侦听器).但是,当我在调用 AnnotationTransformer 之前尝试读取套件文件中的参数时,我无法这样做.
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.
这没有帮助:
<listeners preserve-order="true">
<listener class name="automation.test.SentinelSuiteListener" />
<listener class-name="automation.test.AnnotationTransformer" />
</listeners>
此外,当我尝试同时实现这两个接口时, AnnoataionTransformer 的方法,即转换在 onStart() 方法之前:
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
}
}
推荐答案
您看到的行为是预期的行为,因为 TestNG 是按部就班地工作.
The behavior you seen is the expected one because TestNG works step by step.
首先,它查找测试并读取它们的数据.如果你想改变它们,你可以被触发,这是 IAnnotationTransformer
的目标.
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
.
然后,TestNG 运行suite/tests/classes/methods,您可以使用ISuiteListener
或其他方式触发它.
Then, TestNG runs suite/tests/classes/methods and you can be triggered by that with ISuiteListener
or else.
您需要的是 IMethodInstance
在运行期间调用:http://testng.org/doc/documentation-main.html#methodinterceptors
What you need is IMethodInstance
which is called during the run: http://testng.org/doc/documentation-main.html#methodinterceptors
仅供参考,
节点不接受 preserve-order
属性.请参阅文档:http://testng.org/testng-1.0.dtd.php
Fyi, the <listeners>
node doesn't accept preserve-order
attribute. See the documentation: http://testng.org/testng-1.0.dtd.php
这篇关于我可以订购我的 TestNG 监听器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!