该帖转自其他出处

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?的更多相关文章

  1. AndroidStudio — Error:Failed to resolve: junit:junit:4.12错误解决

    原博客:http://blog.csdn.net/u013443865/article/details/50243193 最近使用AndroidStudio出现以下问题: 解决:打开app下的buil ...

  2. Failed to resolve: junit:junit:4.12

    在Android Studio创建项目之后,提示一个junit错误. 解决方案: 第一步:找到build.gradle的file,如图:  第二步: 第三步:把中间行代码"testCompi ...

  3. pytest文档8-html报告报错截图+失败重跑

    前言 做web自动化的小伙伴应该都希望在html报告中展示失败后的截图,提升报告的档次,pytest-html也可以生成带截图的报告. conftest.py 1.失败截图可以写到conftest.p ...

  4. 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 ...

  5. 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 ...

  6. junit+maven单元测试

    一.概念 junit是一个专门测试的框架 集合maven进行单元测试,可批量测试类中的大量方法是否符合预期 二.作用:单元测试:测试的内容是类中的方法,每一个方法都是独立测试的.方法是测试的基本单位. ...

  7. 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 ...

  8. Junit使用GroboUtils进行多线程测试

    写过Junit单元测试的同学应该会有感觉,Junit本身是不支持普通的多线程测试的,这是因为Junit的底层实现上,是用System.exit退出用例执行的.JVM都终止了,在测试线程启动的其他线程自 ...

  9. 教你快速写出多线程Junit单元测试用例 - GroboUtils

    摘自: http://mushiqianmeng.blog.51cto.com/3970029/897786/ 本文出自One Coder博客,转载请务必注明出处: http://www.coderl ...

随机推荐

  1. C#日期格式字符串的相互转换

    方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss ================================== ...

  2. C#项目中关于多个程序集下App.config文件的问题

    在项目中我们会经常用到App.config文件,有的是自动生成的,比如引用webservice.wcf服务时生成:也有手动建立的配置文件直接默认名就为app.config.这些配置有的保存当前程序集用 ...

  3. java.lang.NoClassDefFoundError: org/hibernate/QueryTimeoutException

    在做ssh整合的时候报错:java.lang.NoClassDefFoundError: org/hibernate/QueryTimeoutException org.springframework ...

  4. linux脚本文件执行的方法之间的区别

    sh/bash sh a.sh bash a.sh 都是打开一个subshell去读取.执行a.sh,而a.sh不需要有"执行权限",在subshell里运行的脚本里设置变量,不会 ...

  5. 软工网络15团队作业4——Alpha阶段敏捷冲刺8.0

    软工网络15团队作业4--Alpha阶段敏捷冲刺8.0 1.每天举行站立式会议,提供当天站立式会议照片一张. 2.项目每个成员的昨天进展.存在问题.今天安排. 2.1 任务完成安排: 成员 昨日已完成 ...

  6. 获取数据库连接对象Connection

    2018-11-04  19:50:52 开始写 public Connection getConn() {//返回类型为Connection try { Class.forName("co ...

  7. GitHub 代码上传

    方法一 登录GitHub后,点击下面的图 New responsitory 按钮 或者点击绿色按钮 New repository,新建一个新建一个远程仓库(remote repository),点击后 ...

  8. ASP.Net 获取Form表单值

    新建一HtmlPage1.html,如下post发送() <body> <form enctype="multipart/form-data" action=&q ...

  9. 【Hbase学习之三】Hbase Java API

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-2.6.5 hbase-0.98.12.1-h ...

  10. mybatis源码解析9---执行器Executor解析

    从前面分析我们知道了sql的具体执行是通过调用SqlSession接口的对应的方法去执行的,而SqlSession最终都是通过调用了自己的Executor对象的query和update去执行的.本文就 ...