我有一种情况需要根据来自我的套件文件的参数启用/禁用测试(使用AnnotationTransformer监听器)(使用ISuiteListener监听器)。但是,当我在调用AnnotationTransformer之前尝试读取套件文件中的参数时,我无法这样做。
这没有用:
<listeners preserve-order="true">
<listener class name="automation.test.SentinelSuiteListener" />
<listener class-name="automation.test.AnnotationTransformer" />
</listeners>
此外,当我尝试同时实现这两个接口时,AnnoataionTransformer的方法即转换在onStart()方法之前:
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
}
}
答案 0 :(得分:2)
您看到的行为是预期的行为,因为TestNG一步一步地工作。
首先,它寻找测试并读取他们的数据。如果您想要更改它们,可以触发它,这是IAnnotationTransformer
的目标。
然后,TestNG运行套件/测试/类/方法,您可以使用ISuiteListener
或其他方式触发。
您需要的是在运行期间调用的IMethodInstance
:http://testng.org/doc/documentation-main.html#methodinterceptors
Fyi,<listeners>
节点不接受preserve-order
属性。请参阅文档:http://testng.org/testng-1.0.dtd.php