本文共 1012 字,大约阅读时间需要 3 分钟。
到目前为止(TestNG 6.9.10),TestNG提供了三个IAnnotationTransformer系列的监听器,这3个监听器的目的都是在TestNG执行过程中动态改变测试类中Annotation的参数。其区别在于不同的IAnnotationTransformer监听器,能够操作的Annotation不同,具体情况如下:
下面以最常用的IAnnotationTransformer监听器为例,介绍具体用法。
1.定制监听器
public class MyTransformer implements IAnnotationTransformer { public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) { if ("invoke".equals(testMethod.getName())) { annotation.setInvocationCount(5); } }}
注意:IAnnotationTransformer监听器接口只有一个方法,该方法的第一个参数为ITestAnnotation,指定了所能够操作的标注为@Test。
而对于IAnnotationTransformer2监听器接口,其中有3个重载方法,每个方法的第一个参数不同,分别为IConfigurationAnnotation、IDataProviderAnnotation和IFactoryAnnotation。
对于IAnnotationTransformer3监听器接口,其中有一个重载方法,该方法的第一个参数为IListenersAnnotation,指定了所能够操作的标注为@Listeners。
2.启动监听器java org.testng.TestNG -listener MyTransformer testng.xml
转载地址:http://gslai.baihongyu.com/