2017-09-25 145 views
0

我在项目中有一些不稳定的测试,像'@Test(retryAnalyzer = RetryAnalyzer.class)'(runner-TestNG)。在RetryAnalyzer类中实现了重试逻辑。测试NG注释扩展

如何创建将扩展@Test注释的自定义注释,并且将具有retryAnalyzer的默认值? (@Test(retryAnalyzer = RetryAnalyzer.class) - > @UnstableTest)

谢谢。

回答

1

这是一个简单的方法。

  • (你的情况@UnstableTest)创建自定义的注释
  • 建设的org.testng.IAnnotationTransformer的实现,其中你测试进来作为transform()方法的参数Method对象,看看它是否有你的注释,如果是,然后注入注释。

下面是它怎么能是这样的:

标记注释,这标志着片状测试

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME) 
@Target({METHOD}) 
@interface UnstableTest {} 

注释变压器

public static class UnstableTestInjector implements IAnnotationTransformer { 

    @Override 
    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) { 
     if (testMethod == null) { 
      return; 
     } 

     UnstableTest unstableTest = testMethod.getAnnotation(UnstableTest.class); 
     if (unstableTest == null) { 
      return; 
     } 
     annotation.setRetryAnalyzer(TryAgain.class); 
    } 
} 

现在,使用添加此监听器<listeners>标签。

应该这样做。