testng实现场景恢复
自动化测试过程中存在很多的不稳定性,例如网络的不稳定,浏览器无响应等等,这些失败往往并不是产品中的错误。那么这时我们需要对执行失败的场景恢复重新执行,确认其是否确实失败。
以前使用QTP的时候也使用了场景恢复,那么testng的场景恢复怎么做呢?
一、查看testng现在接口
首先,我们来看一下TestNG的IRetryAnalyzer接口(因为我的项目是用maven管理,所以接口位置是:Maven Dependencies-testng.jar-org.testng-IRetryAnalyzer.class)
package org.testng; /**
* Interface to implement to be able to have a chance to retry a failed test.
*
* @author tocman@gmail.com (Jeremie Lenfant-Engelmann)
*
*/
public interface IRetryAnalyzer { /**
* Returns true if the test method has to be retried, false otherwise.
*
* @param result The result of the test method that just ran.
* @return true if the test method has to be retried, false otherwise.
*/
public boolean retry(ITestResult result);
}
添加类TestngRetry,实现如下:
/**
* @author Helen
* @date 2018年5月19日
*/
package common; import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
import org.testng.Reporter; /**
* 描述:重写testngRetry接口,设置场景恢复
*/
public class TestngRetry implements IRetryAnalyzer {
private int retryCount = 1;
private static int maxRetryCount = 3;// 最大重新执行场景的次数 /*
* 场景恢复设置,重新执行失败用例的次数
*/
public boolean retry(ITestResult result) {
if (retryCount <= maxRetryCount) {
String message = "Retry for [" + result.getName() + "] on class [" + result.getTestClass().getName()
+ "] Retry " + retryCount + " times";
Reporter.setCurrentTestResult(result);
Reporter.log(message);//报告中输出日志
retryCount++;
return true;
}
return false;
} }
三、添加监听
这时我们还要通过接用IAnnotationTransformer来实现监听,添加类TestngRetryListener,代码如下:
/**
* @author Helen
* @date 2018年5月19日
*/
package common; import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import org.testng.IAnnotationTransformer;
import org.testng.IRetryAnalyzer;
import org.testng.annotations.ITestAnnotation; /**
* 描述:实现IAnnotationTransformer接口,设置监听
*/
public class TestngRetryListener implements IAnnotationTransformer{ public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
IRetryAnalyzer retry = annotation.getRetryAnalyzer();
if (retry == null) {
annotation.setRetryAnalyzer(TestngRetry.class);
} } }
四、配置testng监听器
最后,我们只要在testng.xml里面设置监听就可以了,在testng.xml中添加如下配置:
<listeners>
<!-- 添加场景恢复的监听器 -->
<listener class-name="common.TestngRetryListener"></listener>
</listeners>
五、结果展示
执行完结果后,查看测试报告,测试是有失败的。

在 log输出中,我们可以看到TClassManageTest中的方法inputClassList是重跑了三次的。

testng实现场景恢复的更多相关文章
- QTP 场景恢复– 函数调用
创建自动化测试是为了实现无人值守下运行,但也给开发人员带来一些问题.假如你离开办公室前启动测试,想要让它通宵运行.然而,由于不可预见的错误,您的测试会在某一点停止,中断了测试结果.因此QTP中引入场景 ...
- UFT场景恢复
场景恢复: 在脚本运行中可能会出现一些非预期事件.错误.程序崩溃等情况,阻止脚本继续执行下去,在此情况下脚本可能暂停执行, 直到某些界面被操作之后才会继续执行下去,为了处理这一类事件因此存在场景恢复. ...
- Oracle丢失重做日志的几种场景恢复
实验环境:RHEL6.4 + Oracle 11.2.0.4 一.丢失重做日志组中成员 1.1 故障模拟 1.2 处理方法 1.3 实际处理过程 二.丢失重做日志组 2.1 丢失INACTIVE重做日 ...
- 实验Oracle数据文件被误删除的场景恢复
环境:RHEL 5.4 + Oracle 11.2.0.3 背景:数据库没有备份,数据库文件被误操作rm,此时数据库尚未关闭,也就是对应句柄存在,如何快速恢复? 1.某个普通数据文件被删除 2.所有数 ...
- oracle rm -fr datafile 数据文件被误删的场景恢复(没有rman备份)
环境: Linux release 7.5 oracle19c (无pdb,从11.2.0.4升级上去的) 一:单个非系统表空间的数据文件被删除 我先备份一下,虽然是测试环境. [oracle@19c ...
- QTP场景恢复函数
public Function RecoveryFunction1(Object, Method, Arguments, retVal) Dim FileName ,TimeNow, ResPath ...
- OCP考点实战演练01-备份恢复篇
本系列宗旨:真正掌握OCP考试中所考察的技能,坚决不做Paper OCP! 实验环境:RHEL 6.4 + Oracle 11.2.0.4 OCP考点实战演练01-备份恢复篇 1.数据库开启归档 2. ...
- QTP场景恢复之用例失败自动截图
以下代码是在QC里运行QTP来执行脚本过程,当执行过程中发现用例失败后就会自动截图,然后把用例返回到最初始的状态,模拟了场景恢复的机制 Class QCImageErrorCapture Dim qt ...
- 【QTP-场景恢复】Post-Recovery Test Run Options Screen
Post-Recovery Test Run Options Screen When you clear the Add another recovery operation check box in ...
随机推荐
- jQuery:多个AJAX/JSON请求对应单个回调
原文链接:jQuery: Multiple AJAX and JSON Requests, One Callback 原文日期: 2014年4月15日 翻译日期: 2014年4月22日 翻译人员: 铁 ...
- OpenCV 实现分水岭算法
种子点的标记没有太搞懂,这个算法的速度还是很快的 // watershed_test20140801.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h&q ...
- Xcode中iOS模拟器程序中的plist路径
Xcode6.4里写了个简单的iOS程序在模拟器中跑. 其中用到了NSUserDefaults来保存属性文件plist,那么这个文件实际路径在哪里呢?在网上搜了一下,发现几种说法(*表示当前用户名): ...
- Gathering Initial Troubleshooting Information for Analysis of ORA-4031 Errors on the Shared Pool
In this Document Purpose Troubleshooting Steps References APPLIES TO: Oracle Database - Enterp ...
- android 应用模式之mvp
说到MVP就不得不提到MVC,做过J2EE的猿友们肯定知道MVC是个什么东西.MVC即 Model.View.Controller, 那MVP就Model.View.Presenter.Model用于 ...
- Lease问题
经过查明原来是lease引发的问题.不过查问题的过程让我们耽误了很多修复故障的时间,很是不爽. 起因:datanode和regionserver以及master同时挂掉 现象:datanode重启后, ...
- Linux内核中断和异常分析(下)
这节,我们继续上,中(以前的日志有)篇目进行分析,结合一个真实的驱动案例来描述linux内核中驱动的中断机制,首先我们先了解一下linux内核中提供的中断接口. 这个接口我们需要包含一个头文件:#in ...
- rails小重构:将图片加入产品Model
原先的产品product模式中存放的是图片的url,必须手动将图片存入指定目录中.现在略作改动,在数据库中新建一个pictures表,其设定如下: class CreatePictures < ...
- mysql统计类似SQL语句查询次数
mysql统计类似SQL语句查询次数 vc-mysql-sniffer 工具抓取的sql分析. 1.先用shell脚本把所有enter符号替换为null,再根据语句前后的字符分隔语句 grep -Ev ...
- GitHub awesome Resource
各种Awesome技术资源的资源聚合: https://github.com/sindresorhus/awesome Contents Platforms Programming Languages ...