• Testng监听器




               
                   
               

       
           
               
                   
                       
                   

               

           

       

    import com.welab.automation.framework.uils.PropertiesReader;
    import org.testng.ITestResult;
    import org.testng.util.RetryAnalyzerCount;

    public class TestRetryAnalyzer extends RetryAnalyzerCount {
      private static int count =1;
      public TestRetryAnalyzer() {
        super.setCount(count);
      }

      @Override
      public boolean retryMethod(ITestResult result) {
        return true;
      }

      public void reSetCount() {
        super.setCount(count);
      }
    }


    import com.welab.automation.framework.utils.entity.api.JsonEntity;
    import com.welab.automation.framework.utils.entity.api.TestCaseUtils;
    import org.testng.*;
    import org.testng.annotations.ITestAnnotation;

    import java.lang.reflect.Constructor;
    import java.lang.reflect.Method;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;

    public class RetryListener extends TestListenerAdapter implements IAnnotationTransformer,IHookable {
      public void transform(
          ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
        IRetryAnalyzer iRetryAnalyzer = annotation.getRetryAnalyzer();
        if (iRetryAnalyzer == null) {
          annotation.setRetryAnalyzer(TestRetryAnalyzer.class);
        }
      }

      @Override
      public void onFinish(ITestContext testContext) {
        Iterator listOfSkippedTests =
            testContext.getSkippedTests().getAllResults().iterator();
        while (listOfSkippedTests.hasNext()) {
          ITestResult skippedTest = listOfSkippedTests.next();
          ITestNGMethod method = skippedTest.getMethod();
          if (testContext.getFailedTests().getResults(method).size() > 0
              || testContext.getPassedTests().getResults(method).size() > 0) {
            skippedTest.setStatus(0);
            listOfSkippedTests.remove();
          }
        }
      }

    //iHookable重写方法run

      @Override
      public void run(IHookCallBack iHookCallBack, ITestResult iTestResult) {
        JsonEntity jsonEntity = getJsonEntity(iTestResult);
        Map> sqlExpression = null;
        //replace parameter
        if (jsonEntity != null) {
          jsonEntity.setJsonObject(TestCaseUtils.replaceParameter(jsonEntity.getJsonObject()));
          jsonEntity.updateHeader();
        }
        //execute test case
        iHookCallBack.runTestMethod(iTestResult);
      }

      public JsonEntity getJsonEntity(ITestResult testResult) {
        JsonEntity jsonEntity = null;
        Object[] parameters = testResult.getParameters();

        if (parameters.length > 0) {
          for (Object parameter : parameters) {
            if (parameter instanceof JsonEntity) {
              jsonEntity = (JsonEntity) parameter;
            }
          }
        }
        return jsonEntity;
      }

      @Override
      public void onTestFailure(ITestResult result) {
        TestRetryAnalyzer testRetryAnalyzer = (TestRetryAnalyzer) result.getMethod().getRetryAnalyzer();
        testRetryAnalyzer.reSetCount();
      }
    }
     

    IHookable 监听器提供一种类似于AOP的方式,对测试method进行环绕编程。主要的应用场景:
    1. 动态重写测试method (替换测试方法为空运行)
    2. 动态决定是否跳过测试method (例如鉴权)
    3. 自定义注解,例如@ignore, 当检测到有这个注解的时候,跳过测试执行

    Testng IHookable 监听器_Viogs的博客-CSDN博客

  • 相关阅读:
    LabView中顺序结构的使用
    双目立体视觉
    构建高效沟通桥梁 —— 测试团队中的快速反馈机制建设
    usb hid协议简明简介及使用
    QT内存管理
    tmux随笔
    java计算机毕业设计ssm齐市疫苗管理系统w80jw(附源码、数据库)
    初识设计模式 - 单例模式
    【React组件】github搜索案例之 父子组件通信 (附源码)
    微信、支付宝、百度、抖音开放平台第三方代小程序开发总结
  • 原文地址:https://blog.csdn.net/qq_30273575/article/details/126135212