testng testcase失败重试
简单介绍
需求场景:测试移动端应用,常会因为点击失效、网络延迟大等原因导致测试脚本失败。这时,需要自动重新运行失败的脚本,直到脚本成功通过或者到达限定重试次数。
解决方案:实现testng的IRetryAnalyzer接口。
IRetryAnalyzer
IRetryAnalyzer是testng的一个接口,包含一个retry方法,用于实现失败重试的功能。实现IRetryAnalyzer接口的代码如下:
retry方法的用法是:返回true表示testcase重新运行一次,反之,返回false。
通过自己定义的两个变量retryCount和maxRetryCount来分别记录重试的次数和最多重试的次数。
package main.java.com.dbyl.library.utils; /**
* Created by wwh on 17/2/23.
*/
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult; public class Retry implements IRetryAnalyzer {
private int retryCount = 0;
private int maxRetryCount = 5; // Below method returns 'true' if the test method has to be retried else 'false'
//and it takes the 'Result' as parameter of the test method that just ran
public boolean retry(ITestResult result) {
if (retryCount < maxRetryCount) {
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;
}
}
使用方法
有两种方法使用上面定义的Retry.class:一种是注解,另一种是借助于testng.xml文件。
方法一:通过注解失败重试
修改testcase的注解,由@Test改为@Test(retryAnalyzer = Retry.class)。表示这个testcase使用了失败重试的执行策略。
package main.java.com.dbyl.library.utils; import org.testng.Assert;
import org.testng.annotations.Test; /**
* Created by wwh on 17/2/23.
*/
public class TestRetry { @Test(retryAnalyzer = Retry.class)
public void Demo() {
Assert.fail();
} @Test
public void Demo2(){
Assert.fail();
} @Test
public void Demo3(){
} }
输出结果为:共运行8个testcase,失败了2个(demo和demo2失败了),跳过5个(demo失败后,重试了5次,都失败了,标记为“跳过”),还剩一个成功的是demo3。
[TestNG] Running:
/Users/wwh/Library/Caches/IdeaIC2016.3/temp-testng-customsuite.xml
Retrying test Demo with status FAILURE for the 1 time(s). Test ignored.
Retrying test Demo with status FAILURE for the 2 time(s). Test ignored.
Retrying test Demo with status FAILURE for the 3 time(s). Test ignored.
Retrying test Demo with status FAILURE for the 4 time(s). Test ignored.
Retrying test Demo with status FAILURE for the 5 time(s). Test ignored. java.lang.AssertionError: null at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.fail(Assert.java:101)
。
。
。
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) ===============================================
Default Suite
Total tests run: 8, Failures: 2, Skips: 5
=============================================== Process finished with exit code 0
方法二:通过testng.xml失败重试
与方法一比较,方法二需要再实现一个名为IAnnotationTransformer的接口。这个接口有一个transform方法,用来修改testcase的注解。这个方法的testannotation参数是testcase的注解。通过这个参数可以检查注解中有没有使用RetryAnalyzer,若没有,则将自定义的Retry.class加入到注解中。
package main.java.com.dbyl.library.utils; /**
* Created by wwh on 17/2/23.
*/
import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import org.testng.IAnnotationTransformer;
import org.testng.IRetryAnalyzer;
import org.testng.annotations.ITestAnnotation; public class RetryListener implements IAnnotationTransformer { public void transform(ITestAnnotation testannotation, Class testClass,
Constructor testConstructor, Method testMethod) {
IRetryAnalyzer retry = testannotation.getRetryAnalyzer(); if (retry == null) {
testannotation.setRetryAnalyzer(Retry.class);//检查注解中有没有使用RetryAnalyzer,若没有,则将自定义的Retry.class加入到注解中。
} }
}
接下来,还要在testng.xml中添加刚刚定义的RetryListener这个监听器。
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Second suite" verbose="1" >
<listeners>
<listener class-name="main.java.com.dbyl.library.utils.RetryListener"></listener>
</listeners>
<test name = "allTestsInAClass" >
<classes>
<class name="main.java.com.dbyl.library.utils.TestRetry"/>
</classes>
</test>
</suite>
使用的testng.xml的好处是,可以避免为每个需要重试的testcase添加注解,一切都在配置文件里完成。
这里把RetryListener这个监听器应用到了TestRetry这个类上,所以demo和demo2都会失败重试。
输出结果如下:
[TestNG] Running:
/Users/wwh/IdeaProjects/ProjectWang/src/main/resources/testng.xml
Retrying test Demo with status FAILURE for the 1 time(s). Test ignored.
Retrying test Demo with status FAILURE for the 2 time(s). Test ignored.
Retrying test Demo with status FAILURE for the 3 time(s). Test ignored.
Retrying test Demo with status FAILURE for the 4 time(s). Test ignored.
Retrying test Demo with status FAILURE for the 5 time(s). Test ignored. java.lang.AssertionError: null at org.testng.Assert.fail(Assert.java:94)
。
。
。
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) Retrying test Demo2 with status FAILURE for the 1 time(s). Test ignored.
Retrying test Demo2 with status FAILURE for the 2 time(s). Test ignored.
Retrying test Demo2 with status FAILURE for the 3 time(s). Test ignored.
Retrying test Demo2 with status FAILURE for the 4 time(s). Test ignored.
Retrying test Demo2 with status FAILURE for the 5 time(s). Test ignored. java.lang.AssertionError: null at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.fail(Assert.java:101)
。
。
。
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) ===============================================
Second suite
Total tests run: 13, Failures: 2, Skips: 10
=============================================== Process finished with exit code 0
testng testcase失败重试的更多相关文章
- TestNg失败重试机制
TestNg提供了失败重试接口IRetryAnalyzer,需要实现retry方法: package com.shunhe.testngprac.retry; import org.testng.IR ...
- python unittest case运行失败重试
因为使用unittest进行管理case的运行.有时case因为偶然因素,会随机的失败.通过重试机制能够补充保持case的稳定性.查阅资料后发现,python的unittest自身无失败重试机制,可以 ...
- 稳定UI运行结果-自动化测试失败重试和截图
运行自动化测试的时候,有时会因为网络不稳定,测试环境或者第三方环境正在重启而造成用例运行结果不稳定,时而能跑过时而跑不过.这些难以重现的环境因素造成的用例失败会让测试人员很困扰,排查即耗费时间也没有太 ...
- testng增加失败重跑机制
注: 以下内容引自 http://www.yeetrack.com/?p=1015 testng增加失败重跑机制 Posted on 2014 年 10 月 31 日 使用Testng框架搭建自动测试 ...
- 使用Python请求http/https时设置失败重试次数
设置请求时的重试规则 import requests from requests.adapters import HTTPAdapter s = requests.Session() a = HTTP ...
- 5.如何基于 dubbo 进行服务治理、服务降级、失败重试以及超时重试?
作者:中华石杉 面试题 如何基于 dubbo 进行服务治理.服务降级.失败重试以及超时重试? 面试官心理分析 服务治理,这个问题如果问你,其实就是看看你有没有服务治理的思想,因为这个是做过复杂微服务的 ...
- 面试系列26 如何基于dubbo进行服务治理、服务降级、失败重试以及超时重试
(1)服务治理 1)调用链路自动生成 一个大型的分布式系统,或者说是用现在流行的微服务架构来说吧,分布式系统由大量的服务组成.那么这些服务之间互相是如何调用的?调用链路是啥?说实话,几乎到后面没人搞的 ...
- 重新整理 .net core 实践篇————polly失败重试[三十四]
前言 简单整理一下polly 重试. 正文 在开发程序中一般都有一个重试帮助类,那么polly同样有这个功能. polly 组件包: polly 功能包 polly.Extensions.Http 专 ...
- 『德不孤』Pytest框架 — 5、Pytest失败重试
Pytest失败重试就是,在执行一次测试脚本时,如果一个测试用例执行结果失败了,则重新执行该测试用例. 前提: Pytest测试框架失败重试需要下载pytest-rerunfailures插件. 安装 ...
随机推荐
- 关于IIS的IUSER和IWAM帐户
IUSER是Internet 来宾帐户匿名访问 Internet 信息服务的内置帐户 IWAM是启动 IIS 进程帐户用于启动进程外的应用程序的 Internet 信息服务的内置帐户 (在白吃点就是启 ...
- MQ学习-RabbitMQ, ActiveMQ, Kafka等
之前学习过RabbitMQ,并且还安装过.安装记录的文章如下: Erlang:http://www.cnblogs.com/charlesblc/p/5512380.html RabbitMQ:htt ...
- ArrayList和HashSet的比较
ArrayList是数组存储的方式 HashSet存储会先进行HashCode值得比较(hashcode和equals方法),若相同就不会再存储 HashCode和HashSet类 Hashset就是 ...
- Android开发有用的三方网站
聚合数据-免费数据调用 https://www.juhe.cn/ 有赞- 免费的微商城 http://youzan.com/ 秀米微信图文编辑器 http://xiumi.us/ 禅道项目管理软件 h ...
- 环信ONE SDK架构介绍
环信ONE SDK架构介绍 摘要 环信即时通讯SDK自2014年6月正式公布2.0版本号至今已走过一个年头.从主要的单聊功能,到群聊功能,再到聊天室的实现.SDK无论是功能.稳定性,还是易集成性都在一 ...
- python:如何判断字符串中的内容是否都为数字并且把字符串转换为数字
使用str.isdigit();有两种使用方法 str.isdigit('12345') =====>True str.isdigit('aaaaa')======>False 或者 '1 ...
- Oracle 在Drop表时的Cascade Constraints
http://hi.baidu.com/rebooo/item/12b500b130022bf263388e69假设A为主表(既含有某一主键的表),B为从表(即引用了A的主键作为外键).则当删除A表时 ...
- 字符串(string)操作的相关方法
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 聊聊高并发(三十九)解析java.util.concurrent各个组件(十五) 理解ExecutorService接口的设计
上一篇讲了Executor接口的设计,目的是将任务的运行和任务的提交解耦.能够隐藏任务的运行策略.这篇说说ExecutorService接口.它扩展了Executor接口,对Executor的生命周期 ...
- msgsnd的一个小问题
今天写了一个System V消息队列的小样例.定义了一个例如以下的结构体: #define MSG_SIZE 8192 struct request { long mtype; int client_ ...