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 ...
随机推荐
- Comparator与Comparable用法与区别
一.概述. Comparator和Comparable两者都属于集合框架的一部分,都是用来在对象之间进行比较的,但两者又有些许的不同,我们先通过一个例子来看一下他们的区别,然后再分别学习下它们的源 ...
- Protobuf数据类型
protobuf编译文件和源码在点击打开链接 1: 数据类型: double: 浮点数 float: 单精度浮点 int32: int类型,使用可变长编码,编码负数不够高效,如果有负数那么使用si ...
- ling join 报错The specified LINQ expression contains references to queries that are associated with different cont
The specified LINQ expression contains references to queries that are associated with different cont ...
- pycharm python3.5 神奇的导入问题
说明:pycharm目录中没有同名.py文件,则可以直接用import util导入: 若有同名.py文件,则导入的时候需要加入所在文件夹名称
- anconda1.8+cuda9.0+cudnn7.0.5+tensorflow1.7(win10)安装
1.下载安装cuda9.0 https://developer.nvidia.com/cuda-90-download-archive 2.下载cudnn7.0.5,下载cuda9.0的对应版本 ht ...
- plsql连接远程oracle数据库
1.在oracle安装目录D:\app\Eric\product\11.2.0\dbhome_1\NETWORK\ADMIN找到tnsnames.ora:2.ORCL =(DESCRIPTION = ...
- Spring tokenizeToStringArray
tokenizeToStringArray: StringUtils.tokenizeToStringArray(pattern, this.pathSeparator, this.trimToken ...
- bash 替换特殊字符
bash 替换特殊字符 PID=`netstat -tpln|grep `;PID=${PID#*LISTEN};PID=`echo $PID | sed -s "s/\/java//g&q ...
- Vue + vant-UI 打造移动商城
- 解读 JavaScript 之引擎、运行时和堆栈调用
https://www.oschina.net/translate/how-does-javascript-actually-work-part-1 随着 JavaScript 变得越来越流行,很多团 ...