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 ...
随机推荐
- [提权] 脏牛漏洞 Dirty COW CVE-2016-5195 2.6.22 < 3.9 (x86/x64)
/* * (un)comment correct payload first (x86 or x64)! * * $ gcc cowroot.c -o cowroot -pthread * $ ./c ...
- HashMap(JDK1.8)
四个构造函数: 构造方法只是赋值属性初始值但是不会真正初始化数组表空间,在第一次添加元素时形成数组表空间.这个和以往的jDK1.7之前的不同,1.7之前的都是在构造里初始化了table数组空间. ...
- 手动创建mfc工程(留存方便复制)
案例一. #include <afxwin.h> class CMyWnd : public CWnd { //DECLARE_DYNCREATE(CMyWnd) public: CMyW ...
- ReactiveCocoa(I)
ReactiveCocoa常见类 1. RAC中最核心的类RACSiganl: RACSiganl:信号类,一般表示将来有数据传递,只要有数据改变,信号内部接收到数据,就会马上发出数据 解析: 信号类 ...
- 【2017-03-02】C#集合,结构体,枚举
集合 集合与数组的区别 数组:同一类型,固定长度 集合:不同类型,不固定长度 使用集合前需要: 引用命名空间:using System.Collections; 1.普通集合 定义: Arra ...
- KKT条件原理
问题引入 max f(x, y) s.t. g(x,y) <= 0 几何解释 a. g(x ,y) <= 0为上图中z = 0平面中的圆,圆的边表示g(x, y) = 0,圆的内部表示g ...
- Codeforces 937A - Olympiad
A. Olympiad 题目链接:http://codeforces.com/problemset/problem/937/A time limit per test 1 second memory ...
- AtCoder Beginner Contest 045 B - 3人でカードゲームイージー / Card Game for Three (ABC Edit)
Time limit : 2sec / Memory limit : 256MB Score : 200 points Problem Statement Alice, Bob and Charlie ...
- [转载] Web Service工作原理及实例
一.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...
- [转载]Oracle数据库基础--SQL查询经典例题
Oracle基础练习题,采用Oracle数据库自带的表,适合初学者,其中包括了一些简单的查询,已经具有Oracle自身特点的单行函数的应用 本文使用的实例表结构与表的数据如下: emp员工表结构如下: ...