How to Rerun Failed Tests in JUnit?
该帖转自其他出处
Sometimes due to some temporarily problems such as connection problems, server problems, browser issues, mobile application crashes and freezes and so on our tests fail. In these kinds of situations, we may want to rerun our tests automatically. But how? We can handle this with test frameworks such as TestNG and JUnit. In this post, I want to show you how to solve this problem with JUnit. Also, you can do the same operation with TestNG. It is a great test framework and actually more QA friendly. In another post, I will also explain how to do the same operation with TestNG using several ways. Especially, you can handle many situations with TestNG Listeners and this is another post topic.
In my Junit Rules post, I described how to write Custom Rules and I showed a sample custom ScreenShot Rule implementation. In this post, we will create a similar custom Rule class which implements TestRule class. We need to override evaluate() method and write retry logic in it.
Let’s do an example and see how it works?
We need two classes, one of them is our Rule Class ->> RetryRule and the other is our Test Class ->>RetryRuleTest.
In RetryRuleTest class, I will open www.swtestacademy.com and get its title and check it with WRONG expected title. Thus, our test will fail and I will expect that our test rerun according to given retry count argument. I set retry count as 3 in our example.
RetryRule Class:
package junitexamples.junitrules; /**
* Created by ONUR BASKIRT on 27.03.2016.
*/
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement; public class RetryRule implements TestRule {
private int retryCount; public RetryRule (int retryCount) {
this.retryCount = retryCount;
} public Statement apply(Statement base, Description description) {
return statement(base, description);
} private Statement statement(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Throwable caughtThrowable = null; // implement retry logic here
for (int i = 0; i < retryCount; i++) {
try {
base.evaluate();
return;
} catch (Throwable t) {
caughtThrowable = t;
// System.out.println(": run " + (i+1) + " failed");
System.err.println(description.getDisplayName() + ": run " + (i + 1) + " failed.");
}
}
System.err.println(description.getDisplayName() + ": giving up after " + retryCount + " failures.");
throw caughtThrowable;
}
};
}
}
RetryRuleTest Class:
package junitexamples.junitrules; import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat; /**
* Created by ONUR BASKIRT on 27.03.2016.
*/
public class RetryRuleTest { static WebDriver driver;
final private String URL = "http://www.swtestacademy.com"; @BeforeClass
public static void setupTest(){
driver = new FirefoxDriver();
} //Set retry count argument
@Rule
public RetryRule retryRule = new RetryRule(3); @Test
public void getURLExample() {
//Go to www.swtestacademy.com
driver.get(URL); //Check title is correct
assertThat(driver.getTitle(), is("WRONG TITLE"));
}
}
Result:

How to Rerun Failed Tests in JUnit?的更多相关文章
- AndroidStudio — Error:Failed to resolve: junit:junit:4.12错误解决
原博客:http://blog.csdn.net/u013443865/article/details/50243193 最近使用AndroidStudio出现以下问题: 解决:打开app下的buil ...
- Failed to resolve: junit:junit:4.12
在Android Studio创建项目之后,提示一个junit错误. 解决方案: 第一步:找到build.gradle的file,如图: 第二步: 第三步:把中间行代码"testCompi ...
- pytest文档8-html报告报错截图+失败重跑
前言 做web自动化的小伙伴应该都希望在html报告中展示失败后的截图,提升报告的档次,pytest-html也可以生成带截图的报告. conftest.py 1.失败截图可以写到conftest.p ...
- Selenium 15: How to Run Parallel Tests Using Selenium Grid and JUnit
In this post, I will give two techniques and describe how to run your selenium tests in parallel by ...
- Maven install报错:MojoFailureException ,To see the full stack trace of the errors, re-run Maven with the -e switch.解决
报错日志: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".SLF4J: Defaulting to ...
- junit+maven单元测试
一.概念 junit是一个专门测试的框架 集合maven进行单元测试,可批量测试类中的大量方法是否符合预期 二.作用:单元测试:测试的内容是类中的方法,每一个方法都是独立测试的.方法是测试的基本单位. ...
- How to Use JUnit With JMeter
Do you need to use JUnit in your testing processes? To answer this question, let's take a look first ...
- Junit使用GroboUtils进行多线程测试
写过Junit单元测试的同学应该会有感觉,Junit本身是不支持普通的多线程测试的,这是因为Junit的底层实现上,是用System.exit退出用例执行的.JVM都终止了,在测试线程启动的其他线程自 ...
- 教你快速写出多线程Junit单元测试用例 - GroboUtils
摘自: http://mushiqianmeng.blog.51cto.com/3970029/897786/ 本文出自One Coder博客,转载请务必注明出处: http://www.coderl ...
随机推荐
- MyBatis基础入门《四》接口方式.Select查询集合
MyBatis基础入门<四>接口方式.Select查询集合 描述: 在<MyBatis基础入门<二>Select查询>中有说过,SQLSession有两种用法,这里 ...
- 更改file文件上传默认CSS样式
前言: 多数时候我们需要表单上传文件,如图片.但是浏览器默认的input[file]样式很不友好, 需要我们自己手动修改. 如图基于bootstrap布局的表单, 但file文件上传样式不敢恭维. & ...
- JavaScript数组去重方法总结
一.双重遍历去重 function onlyFigure(arr) { let newarr = []; const length = arr.length for (let i = 0; i < ...
- JAVA_Stream_练习
package airycode_java8.nice7; import airycode_java8.nice1.Employee; import org.junit.Test; import ja ...
- c#除掉字符串最后一个字符几种方法
有一数组:转换为字符串后为 aaa|bbb|ccc|ddd| 现要去掉最后一个| 第一种方法: 语句为:str1=aaa|bbb|ccc|ddd| str=str1.substring(0,lasti ...
- lumisoft.net 邮件管理系列文章 - 如何判断附件为内嵌式还是附加式
如果要区分邮件里面的附件是内嵌图片附件还是真正的附件,那么可以通过下面代码进行判断,如果是MIME_DispositionTypes.Attachment的就是普通附件,MIME_Dispositio ...
- WEB应用程序:AJAX全套
概述 对于WEB应用程序:用户浏览器发送请求,服务器接收并处理请求,然后返回结果,往往返回就是字符串(HTML),浏览器将字符串(HTML)渲染并显示浏览器上. 1.传统的Web应用 一个简单操作需要 ...
- 多线程:Operation(一)
1. 进程和线程 1.1 进程 进程:正在运行的应用程序叫进程 进程之间都是独立的,运行在专用且受保护的内存空间中 两个进程之间无法通讯 通俗的理解,手机上同时开启了两个App.这两个App肯定是在不 ...
- File §2
Previously speaking,File can be seen as one ducument, also can be seen as list of documents like dir ...
- redis相关问题
什么是Redis?Redis 是一个使用 C 语言写成的,开源的 key-value 数据库..和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表 ...