简单介绍

需求场景:测试移动端应用,常会因为点击失效、网络延迟大等原因导致测试脚本失败。这时,需要自动重新运行失败的脚本,直到脚本成功通过或者到达限定重试次数。

解决方案:实现testng的IRetryAnalyzer接口。

IRetryAnalyzer

IRetryAnalyzer是testng的一个接口,包含一个retry方法,用于实现失败重试的功能。实现IRetryAnalyzer接口的代码如下:

retry方法的用法是:返回true表示testcase重新运行一次,反之,返回false。

通过自己定义的两个变量retryCount和maxRetryCount来分别记录重试的次数和最多重试的次数。

  1. package main.java.com.dbyl.library.utils;
  2.  
  3. /**
  4. * Created by wwh on 17/2/23.
  5. */
  6. import org.testng.IRetryAnalyzer;
  7. import org.testng.ITestResult;
  8.  
  9. public class Retry implements IRetryAnalyzer {
  10. private int retryCount = 0;
  11. private int maxRetryCount = 5;
  12.  
  13. // Below method returns 'true' if the test method has to be retried else 'false'
  14. //and it takes the 'Result' as parameter of the test method that just ran
  15. public boolean retry(ITestResult result) {
  16. if (retryCount < maxRetryCount) {
  17. System.out.println("Retrying test " + result.getName() + " with status "
  18. + getResultStatusName(result.getStatus()) + " for the " + (retryCount+1) + " time(s).");
  19. retryCount++;
  20. return true;
  21. }
  22. return false;
  23. }
  24.  
  25. public String getResultStatusName(int status) {//这个函数将状态码转换为状态文字。
  26. String resultName = null;
  27. if(status==1)
  28. resultName = "SUCCESS";
  29. if(status==2)
  30. resultName = "FAILURE";
  31. if(status==3)
  32. resultName = "SKIP";
  33. return resultName;
  34. }
  35. }

使用方法

有两种方法使用上面定义的Retry.class:一种是注解,另一种是借助于testng.xml文件。

方法一:通过注解失败重试

修改testcase的注解,由@Test改为@Test(retryAnalyzer = Retry.class)。表示这个testcase使用了失败重试的执行策略。

  1. package main.java.com.dbyl.library.utils;
  2.  
  3. import org.testng.Assert;
  4. import org.testng.annotations.Test;
  5.  
  6. /**
  7. * Created by wwh on 17/2/23.
  8. */
  9. public class TestRetry {
  10.  
  11. @Test(retryAnalyzer = Retry.class)
  12. public void Demo() {
  13. Assert.fail();
  14. }
  15.  
  16. @Test
  17. public void Demo2(){
  18. Assert.fail();
  19. }
  20.  
  21. @Test
  22. public void Demo3(){
  23. }
  24.  
  25. }

输出结果为:共运行8个testcase,失败了2个(demo和demo2失败了),跳过5个(demo失败后,重试了5次,都失败了,标记为“跳过”),还剩一个成功的是demo3。

  1. [TestNG] Running:
  2. /Users/wwh/Library/Caches/IdeaIC2016.3/temp-testng-customsuite.xml
  3. Retrying test Demo with status FAILURE for the 1 time(s).
  4.  
  5. Test ignored.
  6. Retrying test Demo with status FAILURE for the 2 time(s).
  7.  
  8. Test ignored.
  9. Retrying test Demo with status FAILURE for the 3 time(s).
  10.  
  11. Test ignored.
  12. Retrying test Demo with status FAILURE for the 4 time(s).
  13.  
  14. Test ignored.
  15. Retrying test Demo with status FAILURE for the 5 time(s).
  16.  
  17. Test ignored.
  18.  
  19. java.lang.AssertionError: null
  20.  
  21. at org.testng.Assert.fail(Assert.java:94)
  22. at org.testng.Assert.fail(Assert.java:101)
  23.   
      。 
      。
  24. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  25. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  26. at java.lang.reflect.Method.invoke(Method.java:498)
  27. at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
  28.  
  29. ===============================================
  30. Default Suite
  31. Total tests run: 8, Failures: 2, Skips: 5
  32. ===============================================
  33.  
  34. Process finished with exit code 0

方法二:通过testng.xml失败重试

与方法一比较,方法二需要再实现一个名为IAnnotationTransformer的接口。这个接口有一个transform方法,用来修改testcase的注解。这个方法的testannotation参数是testcase的注解。通过这个参数可以检查注解中有没有使用RetryAnalyzer,若没有,则将自定义的Retry.class加入到注解中。

  1. package main.java.com.dbyl.library.utils;
  2.  
  3. /**
  4. * Created by wwh on 17/2/23.
  5. */
  6. import java.lang.reflect.Constructor;
  7. import java.lang.reflect.Method;
  8.  
  9. import org.testng.IAnnotationTransformer;
  10. import org.testng.IRetryAnalyzer;
  11. import org.testng.annotations.ITestAnnotation;
  12.  
  13. public class RetryListener implements IAnnotationTransformer {
  14.  
  15. public void transform(ITestAnnotation testannotation, Class testClass,
  16. Constructor testConstructor, Method testMethod) {
  17. IRetryAnalyzer retry = testannotation.getRetryAnalyzer();
  18.  
  19. if (retry == null) {
  20. testannotation.setRetryAnalyzer(Retry.class);//检查注解中有没有使用RetryAnalyzer,若没有,则将自定义的Retry.class加入到注解中。
  21. }
  22.  
  23. }
  24. }

接下来,还要在testng.xml中添加刚刚定义的RetryListener这个监听器。

  1. <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
  2. <suite name="Second suite" verbose="1" >
  3. <listeners>
  4. <listener class-name="main.java.com.dbyl.library.utils.RetryListener"></listener>
  5. </listeners>
  6. <test name = "allTestsInAClass" >
  7. <classes>
  8. <class name="main.java.com.dbyl.library.utils.TestRetry"/>
  9. </classes>
  10. </test>
  11. </suite>

使用的testng.xml的好处是,可以避免为每个需要重试的testcase添加注解,一切都在配置文件里完成。

这里把RetryListener这个监听器应用到了TestRetry这个类上,所以demo和demo2都会失败重试。

输出结果如下:

  1. [TestNG] Running:
  2. /Users/wwh/IdeaProjects/ProjectWang/src/main/resources/testng.xml
  3. Retrying test Demo with status FAILURE for the 1 time(s).
  4.  
  5. Test ignored.
  6. Retrying test Demo with status FAILURE for the 2 time(s).
  7.  
  8. Test ignored.
  9. Retrying test Demo with status FAILURE for the 3 time(s).
  10.  
  11. Test ignored.
  12. Retrying test Demo with status FAILURE for the 4 time(s).
  13.  
  14. Test ignored.
  15. Retrying test Demo with status FAILURE for the 5 time(s).
  16.  
  17. Test ignored.
  18.  
  19. java.lang.AssertionError: null
  20.  
  21. at org.testng.Assert.fail(Assert.java:94)
  22.   
      
      
  23. at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
  24.  
  25. Retrying test Demo2 with status FAILURE for the 1 time(s).
  26.  
  27. Test ignored.
  28. Retrying test Demo2 with status FAILURE for the 2 time(s).
  29.  
  30. Test ignored.
  31. Retrying test Demo2 with status FAILURE for the 3 time(s).
  32.  
  33. Test ignored.
  34. Retrying test Demo2 with status FAILURE for the 4 time(s).
  35.  
  36. Test ignored.
  37. Retrying test Demo2 with status FAILURE for the 5 time(s).
  38.  
  39. Test ignored.
  40.  
  41. java.lang.AssertionError: null
  42.  
  43. at org.testng.Assert.fail(Assert.java:94)
  44. at org.testng.Assert.fail(Assert.java:101)
  45.   
      
      
  46. at java.lang.reflect.Method.invoke(Method.java:498)
  47. at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
  48.  
  49. ===============================================
  50. Second suite
  51. Total tests run: 13, Failures: 2, Skips: 10
  52. ===============================================
  53.  
  54. Process finished with exit code 0

testng testcase失败重试的更多相关文章

  1. TestNg失败重试机制

    TestNg提供了失败重试接口IRetryAnalyzer,需要实现retry方法: package com.shunhe.testngprac.retry; import org.testng.IR ...

  2. python unittest case运行失败重试

    因为使用unittest进行管理case的运行.有时case因为偶然因素,会随机的失败.通过重试机制能够补充保持case的稳定性.查阅资料后发现,python的unittest自身无失败重试机制,可以 ...

  3. 稳定UI运行结果-自动化测试失败重试和截图

    运行自动化测试的时候,有时会因为网络不稳定,测试环境或者第三方环境正在重启而造成用例运行结果不稳定,时而能跑过时而跑不过.这些难以重现的环境因素造成的用例失败会让测试人员很困扰,排查即耗费时间也没有太 ...

  4. testng增加失败重跑机制

    注: 以下内容引自 http://www.yeetrack.com/?p=1015 testng增加失败重跑机制 Posted on 2014 年 10 月 31 日 使用Testng框架搭建自动测试 ...

  5. 使用Python请求http/https时设置失败重试次数

    设置请求时的重试规则 import requests from requests.adapters import HTTPAdapter s = requests.Session() a = HTTP ...

  6. 5.如何基于 dubbo 进行服务治理、服务降级、失败重试以及超时重试?

    作者:中华石杉 面试题 如何基于 dubbo 进行服务治理.服务降级.失败重试以及超时重试? 面试官心理分析 服务治理,这个问题如果问你,其实就是看看你有没有服务治理的思想,因为这个是做过复杂微服务的 ...

  7. 面试系列26 如何基于dubbo进行服务治理、服务降级、失败重试以及超时重试

    (1)服务治理 1)调用链路自动生成 一个大型的分布式系统,或者说是用现在流行的微服务架构来说吧,分布式系统由大量的服务组成.那么这些服务之间互相是如何调用的?调用链路是啥?说实话,几乎到后面没人搞的 ...

  8. 重新整理 .net core 实践篇————polly失败重试[三十四]

    前言 简单整理一下polly 重试. 正文 在开发程序中一般都有一个重试帮助类,那么polly同样有这个功能. polly 组件包: polly 功能包 polly.Extensions.Http 专 ...

  9. 『德不孤』Pytest框架 — 5、Pytest失败重试

    Pytest失败重试就是,在执行一次测试脚本时,如果一个测试用例执行结果失败了,则重新执行该测试用例. 前提: Pytest测试框架失败重试需要下载pytest-rerunfailures插件. 安装 ...

随机推荐

  1. 【hibernate】hibernate不同版本的命名策略

    ===================================================hibernate 4命名策略如下================================ ...

  2. 关于IIS的IUSER和IWAM帐户

    IUSER是Internet 来宾帐户匿名访问 Internet 信息服务的内置帐户 IWAM是启动 IIS 进程帐户用于启动进程外的应用程序的 Internet 信息服务的内置帐户 (在白吃点就是启 ...

  3. Web自动化测试框架改进

    Web自动化测试框架(WebTestFramework)是基于Selenium框架且采用PageObject设计模式进行二次开发形成的框架. 一.适用范围:传统Web功能自动化测试.H5功能自动化测试 ...

  4. 性能测试报告模板 V1.0

    1. 测试项目概述与测试目的 1.1 项目概述 本部分主要是针对即将进行压力测试的对象(接口.模块.进程或系统)进行概要的说明,让人明白该测试对象的主要功能与作用及相关背景. 1.2 测试目标 简要列 ...

  5. Andfix热修复框架原理及源代码解析-上篇

    热补丁介绍及Andfix的使用 Andfix热修复框架原理及源代码解析-上篇 Andfix热修复框架原理及源代码解析-下篇 1.不知道怎样使用的同学,建议看看我上一篇写的介绍热补丁和Andfix的使用 ...

  6. 如何让<input type="text" />中的文字居中

    高(height)和行高(line-height)相等.不能用vertical-align

  7. Node.js 抓取电影天堂新上电影节目单及ftp链接

    代码地址如下:http://www.demodashi.com/demo/12368.html 1 概述 本实例主要使用Node.js去抓取电影的节目单,方便大家使用下载. 2 node packag ...

  8. wait和notify实现的生产者消费者线程交互

    public class ProductTest { public static void main(String args[]) { Repertory repertory=new Repertor ...

  9. Android 属性动画框架 ObjectAnimator、ValueAnimator ,这一篇就够了

    前言 我们都知道 Android 自带了 Roate Scale Translate Alpha 多种框架动画,我们可以通过她们实现丰富的动画效果,但是这些宽家动画却有一个致命的弱点,它们只是改变了 ...

  10. rtems 4.11 时钟驱动(arm, beagle)

    根据bsp_howto手册,时钟驱动的框架主要在 c/src/lib/libbsp/shared/Clockdrv_shell.h 文件中实现 时钟初始化 时钟驱动初始化函数为 Clock_initi ...