重写转换方法。从TestNG.xml执行时它工作正常
测试类
@Listeners(Annotation.AnnotationTransform.class)
public class factory {
@
Test(priority = 1, invocationCount = 3)
public void t1() {
System.out.println("Method is t1, parameter is " + "one");
AssertJUnit.assertTrue(true);
}
@
Test(priority = 2)
public void t2() {
System.out.println("Method is t2, parameter is " + "two");
}
@
Test(priority = 3)
public void t3() {
System.out.println("Method is t3");
}
}
听众类
public class AnnotationTransform implements IAnnotationTransformer, IRetryAnalyzer {
@Override
public void transform(ITestAnnotation annotation, Class testClass,
Constructor testConstructor, Method testMethod) {
if (testMethod.getName().equals("t1")) {
System.out.println("set data provider for " + testMethod.getName());
annotation.setInvocationCount(10);
} else if (testMethod.getName().equals("t3")) {
System.out.println("set data provider for " + testMethod.getName());
} else if (testMethod.getName().equals("t2")) {
System.out.println("Disable " + testMethod.getName());
annotation.setEnabled(false);
}
}
}
将上述代码作为“Run as TestNG Test”运行不会调用transform方法 从testNG.xml文件下面的'Run as TestNG Suite'中可以正常使用
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false" >
<listeners>
<listener class-name="Annotation.AnnotationTransform" />
</listeners>
<test name="Test" preserve-order="true">
<classes>
<class name="Annotation.factory" />
</classes>
</test>
</suite>
答案 0 :(得分:2)
您需要提及在testng.xml中实现IAnnotationTransformer的任何侦听器类,因为@listeners注释不会将其拾取。
来自TestNg文档 -
@Listeners注释可以包含任何扩展org.testng.ITestNGListener的类,但IAnnotationTransformer和IAnnotationTransformer2除外。原因是这些侦听器需要在过程的早期就知道,以便TestNG可以使用它们来重写注释,因此您需要在testng.xml文件中指定这些侦听器。
网址 - http://testng.org/doc/documentation-main.html#testng-listeners