到目前为止(TestNG 6.9.10),TestNG提供了三个IAnnotationTransformer系列的监听器,这3个监听器的目的都是在TestNG执行过程中动态改变测试类中Annotation的参数。其区别在于不同的IAnnotationTransformer监听器,能够操作的Annotation不同,具体情况如下:
IAnnotationTransformer,操作@Test标注 IAnnotationTransformer2,操作@Configuration标注、@DataProvider标注和@Factory标注 IAnnotationTransformer3,操作@Listeners标注下面以最常用的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