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 ...
随机推荐
- Browsersync结合gulp和nodemon实现express全栈自动刷新
Browsersync能让浏览器实时.快速响应你的文件更改(html.js.css.sass.less等)并自动刷新页面.更重要的是 Browsersync可以同时在PC.平板.手机等设备下进项调试. ...
- Unity shader学习之Alpha Test
可以在fragment中使用cg的函数--clip来进行透明度测试. 函数定义如下: void clip(float4 x); void clip(float3 x); void clip(float ...
- Oil Deposits HDU 1241
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...
- 爬取笔下wenxue小说
import urllib.request from bs4 import BeautifulSoup import re def gethtml(url): page=urllib.request. ...
- java 3大特性
1. 封装 : 定义: 封装是把过程和数据包围起来,对数据的访问只能通过已定义的接口. 作用: 封装通过对属性增加修饰符来控制数据的访问权限,定义数据的访问接口,增加了数据的隐秘性和安全性. 2.继承 ...
- css 渐变动画
就是这样 这是一段可选文字 -------------------------------------- 代码如下 CSS代码: @media all and (-webkit-min-device- ...
- table 的rolspan和rowspan
如图所示啦,容易让初学者混乱的两个东西仔细看看分析下呢,就比较简单了 <table width="300" border="2"> <tr&g ...
- MySql 存储过程 光标只循环一次
[1]MqSql 存储过程 光标只循环一次 针对MySql存储过程,光标只循环一次就退出的场景,可能原因分析: (1)存储过程有问题(仔细检查语法.控制变量.条件等等) (2)保证存储过程正确.调用过 ...
- STL容器之set
[1]set容器 一个集合(set)是一个容器,它其中所包含的元素的值是唯一的. [2]set容器方法 (1)set构造函数.插入函数.遍历过程 应用示例代码如下: #include <set& ...
- Knowing is not enough; we must apply. Willing is not enough; we must do.
Knowing is not enough; we must apply. Willing is not enough; we must do. 仅限于知道是不够的,我们必须去实践:单纯的希望是不够的 ...