【问题标题】:How do I use IAnnotationTransformer in testNG?如何在 testNG 中使用 IAnnotationTransformer?
【发布时间】:2014-09-10 03:01:28
【问题描述】:

如何在 testNG 中使用 IAnnotationTransformer? 当我调试时,代码从未进入转换函数。它执行了所有 3 个测试。我使用 Maven 来触发我的测试,这就是我所拥有的--

public class SomeTest {

    @BeforeClass
    public void before(){

        TestNG testNG = new TestNG();
        testNG.setAnnotationTransformer(new Transformer());
    }

    @Test(priority = 1)
    public void test1(){}

    @Test(priority = 2)
    public void test2(){}

    @Test(priority = 3)
    public void test3(){}
}

public class Transformer implements IAnnotationTransformer {

    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod){

        if (true){
            annotation.setEnabled(false);
        }
    }
}

【问题讨论】:

    标签: java selenium annotations testng


    【解决方案1】:

    在您使用 serviceloaders 设置监听器之前,它们不会自动被拾取。

    有多种方法可以根据需要指定侦听器。
    如果您不想将它们应用于所有测试,您可以选择在单个类上应用监听器

    例如。

    @Listeners(Transformer.class)
    public class InterceptorTC {
    
    @Test(priority..
    

    您还可以从 maven surefire 插件中指定一个侦听器。阅读使用自定义监听器部分here

    其他指定侦听器的方法可以是在您的suite xml 中定义它,并在 maven 插件中指定您的套件 xml 文件。

    .

    【讨论】:

      【解决方案2】:

      void 变换(ITestAnnotation 注释, java.lang.Class 测试类, java.lang.reflect.Constructor 测试构造器, java.lang.reflect.Method testMethod)

      TestNG 将调用此方法,让您有机会修改从测试类中读取的 TestNG 注释。您可以通过调用 ITest 接口上的任何设置器来更改所需的值。请注意,testClass、testConstructor 和 testMethod 三个参数中只有一个不为空。

      【讨论】:

        【解决方案3】:
        public class Retry implements IRetryAnalyzer {
        
        int retrycount = 0;
         int maxretyrcount =2;
        
        @Override
        public boolean retry(ITestResult result) {
            // TODO Auto-generated method stub
            if (retrycount<maxretyrcount){
                System.out.println("Retrying test " + result.getName() + " with status "
                        + getResultStatusName(result.getStatus()) + " for the " + (retrycount+1) + " time(s).");
                retrycount++;
                return true;
            }
        
            return false;
        }
        
         public String getResultStatusName(int status) {
                String resultName = null;
                if(status==1)
                    resultName = "SUCCESS";
                if(status==2)
                    resultName = "FAILURE";
                if(status==3)
                    resultName = "SKIP";
                return resultName;
            }
        

        }

        // 另一个类 公共类 RetryListner 实现 IAnnotationTransformer{

        @Override
        public void transform(ITestAnnotation arg0, Class arg1, Constructor arg2,Method arg3) {
            // TODO Auto-generated method stub
        
            IRetryAnalyzer analyzer = arg0.getRetryAnalyzer();
        
            if (analyzer == null)   {
                arg0.setRetryAnalyzer(Retry.class);
            }
        
        }
        

        }

        【讨论】:

          【解决方案4】:

          您似乎想禁用您的测试方法,就像代码中的 @test(enable = false) 一样。

          这是一个在运行时修改@Test注解内容的代码sn-p:

          public class Transformer implements IAnnotationTransformer {
          
              @Override
              public void transform(ITestAnnotation annotation, Class testClass,Constructor testConstructor, Method testMethod){      
                 if (testMethod.getName().equals("test1")){
                      System.out.println("Disable " + testMethod.getName());
                      annotation.setEnabled(false);
                  }
              }
          }
          

          当你实现一个TestNG接口时,你可以通过以下任一方式让TestNG知道它:

          1. 在命令行上使用 -listener。
          2. 与蚂蚁一起使用。
          3. 在您的 testng.xml 文件中使用。

          4. 在您的任何测试类上使用 @Listeners 注释。

          5. 使用 ServiceLoader。

          如果您对此有任何疑虑,请告诉我。

          【讨论】:

            猜你喜欢
            • 2021-07-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-06-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多