Junit Framework -TestRule,自动化测试中如何再次运行失败的测试用例
有时由于服务器,浏览器等问题,会导致自动化测试用例运行失败,此处通过案例讲解如何使用Junit框架中的TestRule来实现重复运行失败的测试用例。
首先定义一个类并让它实现TestRule,代码如下:
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement; public class RetryRule implements TestRule{
private final int retryCount;
//构造方法
public RetryRule(int retryCount){
this.retryCount=retryCount;
}
@Override
public Statement apply(Statement base, Description description) {
return statement(base,description);
}
private Statement statement(Statement base, Description description) {
return new Statement() { @Override
public void evaluate() throws Throwable {
Throwable caughtThrowable=null;
for (int i = 0; i < retryCount; i++) {
try {
base.evaluate();
return;
} catch (final Throwable t) {
caughtThrowable=t;
System .err .println(description.getDisplayName()+":run"+(i+1)+"failed.");
}
}
System.err.println(description.getDisplayName()+":giving up after"+retryCount+"failure.");
throw caughtThrowable;
}
};
} }
之后创建一个测试类,该类中选择打开https://www.baidu.com网站,测试网页标题中是否含有123。
import static org.junit.Assert.assertTrue; import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class RetryRuleTest { static WebDriver driver;
private final String URL="https://meitaichina.alpha.tmogroup.asia/"; @BeforeClass
public static void setUpBeforeClass() throws Exception {
driver=new ChromeDriver();
}
@Rule
public RetryRule retryRule=new RetryRule(3); @Test
public void test() {
driver.get("https://www.baidu.com");
assertTrue("faile",driver.getTitle().toString().contains("123"));
} @AfterClass
public static void tearDownAfterClass() throws Exception {
driver.quit();
} }
由于标题中并不存在123所以此时我们可以查看到运行结果,且确实运行了三次
Starting ChromeDriver 2.25.426924 (649f9b868f6783ec9de71c123212b908bf3b232e) on port 10920
Only local connections are allowed.
May 11, 2017 11:09:15 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
test(RetryRuleTest):run1failed.
test(RetryRuleTest):run2failed.
test(RetryRuleTest):run3failed.
test(RetryRuleTest):giving up after3failure.
Junit Framework -TestRule,自动化测试中如何再次运行失败的测试用例的更多相关文章
- cx_Oracle库导入失败引起crontab中python程序运行失败,并且无错误提示
今天遇到一个问题: 一个python脚本命令行运行时很正常,放到crontab中就无法工作,日志也没有记录,找了半天,终于发现问题所在. 在脚本最上方,程序如下: #!/usr/local/bin p ...
- python web自动化测试中失败截图方法汇总
在使用web自动化测试中,用例失败则自动截图的网上也有,但实际能落地的却没看到,现总结在在实际应用中失败截图的几种方法: 一.使用unittest框架截图方法: 1.在tearDown中写入截图的 ...
- Junit测试中找不到junit.framework.testcase
在使用Junit进行测试时,出现如下问题: 找不到junit.framework.testcase 解决方法: 选中项目->属性->Java构建路径->库->添加外部jar 在 ...
- 在UI自动化测试中使用flaky插件运行失败用例
在UI自动化测试中,有时候经常会提示跑用例失败,在单步或单个用例调试时,用例却成功,这个失败的因素主要有环境.代码或前端定位等原因. 可以看这篇文章<我们是如何让UI测试变得稳定的>中有详 ...
- <p>在静态类junit.framework.Assert或者静态类org.junit.Assert中存在下面几个方法</p>
在静态类junit.framework.Assert或者静态类org.junit.Assert中存在下面几个方法 1.assertEquals()方法,用来查看对象中存的值是否是期待的值,与字符串比較 ...
- JAVA自动化测试中多数据源的切换
在做自动化测试时,数据驱动是一个很重要的概念,当数据与脚本分离后,面对茫茫多的数据,管理数据又成了一个大问题,而数据源又可能面对多个,就跟在开发过程中,有时候要连接MYSQL,有时候又要连接SQL S ...
- (转)Web自动化测试中的接口测试
1.背景 1.1 Web程序中的接口 1.1.1 典型的Web设计架构 web是实现了基于网络通信的浏览器客户端与远程服务器进行交互的应用,通常包括两部分:web服务器和web客户端.web客户端的应 ...
- 安装.NET Framework组件时,电脑意外重启后再次安装失败
因为软件运行环境需要安装.Net Framework,我安装的是2.0sp版本,可以安装过程中计算机意外关闭,重新打开后再次安装却出现安装失败的提示,具体内容是: 产品: Microsoft .NET ...
- eclipse 环境 JUnit 测试框架(junit.framework.* 与 org.junit.*)
如下所示,先通过 build path 导入 junit 环境依赖的 jar 包: 1. junit.framework.* junit.framework.* 主要类和函数: Test TestCa ...
随机推荐
- C语言记录汇总
uint32_t 转载自:http://blog.sina.com.cn/s/blog_6aea878e0100tl0f.html体会1>. 在写程序时注意"无符号类型&quo ...
- ****************VS编码操作实践******************
下面是今天主要练习的内容: 运用到的内容有 {运算符.强制转换.数据类型的运用.转义字符.变量与常量.基本类型的转换等} 1) 首先我们来看 下列的编码是由三大类组成的 ① 定制变量与常量 蓝色部 ...
- 获取Sqlserver上一句语句执行后受影响的行数@@rowCount
from:http://blog.163.com/rihui_7/blog/static/212285143201381343240404/ 返回受上一语句影响的行数. ROWCOUNT_BIG.&q ...
- 关于 LD_LIBRARY_PATH 这个环境变量
这个变量中可以保存linux寻找库时搜索的路径,按照一篇文章中的介绍,不应该设置这个变量.文章的重点如下: 1. 不要设置这个变量. 2. Solaris中,在编译时,使用 -L 选项指定编译时库的搜 ...
- ResourceExhaustedError 解决方案
原因:网络层太多,运算量太大导致GPU资源耗尽 解决方案: 1.限制GPU的使用: config = tf.ConfigProto()config.gpu_options.per_process_gp ...
- sql server 将两列的值合并到另一列
select top 100 t2.FullName, * from Subject,(select id, isnull(first_name,'') +isnull(middle_name,'') ...
- linux环境下tab键自动缩进4个空格
1. 进入 root 模式 su root 2. 编辑 /etc/vimrc 文件 root@localhost /home/xiluhua/tscripts $ vi /etc/vimrc 3. 文 ...
- 对SQLite 数据库的一点点了解
SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它的设计目标是嵌入式的,它占用资源非常低,在嵌入式设备中,可能只需要几百k的内存就够了. SQLit ...
- c# 共享事件处理程序
使用同一个方法来处理多个Button实例的Click事件. 1.全选所有的Button,在事件添加中的Click点击事件中添加处理函数. 2.假如一个label控件用于显示按钮按下输出文本 3.处理函 ...
- 微信小程序制作家庭记账本之四
第四天,仍然对记账本代码进行研究,对按钮的大小设置,颜色,具体位置进行分析,但其中很多代码都不明白.