该帖转自其他出处

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() {@Overridepublic void evaluate() throws Throwable {Throwable caughtThrowable = null;// implement retry logic herefor (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";@BeforeClasspublic static void setupTest(){driver = new FirefoxDriver();}//Set retry count argument
    @Rulepublic RetryRule retryRule = new RetryRule(3);@Testpublic void getURLExample() {//Go to www.swtestacademy.com
        driver.get(URL);//Check title is correctassertThat(driver.getTitle(), is("WRONG TITLE"));}
}

Result:

转载于:https://www.cnblogs.com/woniu123/p/6839984.html

How to Rerun Failed Tests in JUnit?相关推荐

  1. 基于nose、使用django实现的自动化测试执行平台实现Rerun Failed功能。

    Windautotest是我开发的一个基于Web界面进行操作的测试用例自动化执行平台.Windautotest使用python+django在nose+unittest的基础上开发.相比于直接使用no ...

  2. Android studio之Error:(23, 17) Failed to resolve: junit:junit:4.12

    1 probleam 2 resolve method // testCompile 'junit:junit:4.12'

  3. AS报Failed to resolve: junit:junit:4.12错误正确的解决方法

    其实这个错误并非掩耳盗铃般的把testCompile 'junit:junit:4.12'注释或删除掉,在stackoverflow中有明确的原由和解决方法,只需在Application的gradle ...

  4. pytest合集(3)— 命令行参数

    1.命令行参数大全 使用 pytest -h 可以查看 pytest 的命令行参数,有 10 大类共 132 个. 详见:Python pytest 132 个命令行参数用法 - 习久性成 - 博客园 ...

  5. 分析自动化测试收益_分析自动化测试结果

    分析自动化测试收益 So, you've set up a sweet automated test framework. You're running it in your CI/CD pipeli ...

  6. Jetbrains Rider 快捷键

    CREATE AND EDIT 操作 Show context actions Alt+Enter CREATE AND EDIT Show context actions Alt+Enter Bas ...

  7. surefire 拉起 junit 单元测试类 源码阅读(一)

    根据surefire 拉起Junit单元测试类 输出的报错日志 跟踪执行过程: 日志1: java.lang.reflect.InvocationTargetExceptionat sun.refle ...

  8. maven失败测试用例rerun插件使用方法

    ​在运行测试用例时,有时候希望本次运行结束后自动运行失败的测试用例,以排除结果由于网络或其他连接原因导致的偶发抖动.通过参考查阅资料,有以下几种方法可以达到目的: ***1.maven的surefir ...

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

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

最新文章

  1. transforms函数查询
  2. 把 textbox 遍历赋值为空
  3. 轴等比缩放_CAD教程:自由缩放命令的操作流程
  4. OpenCV gapi模块基本API的实例(附完整代码)
  5. python爬虫实践 —— 一、入门篇
  6. python基础-第三篇-函数编程
  7. 10双屏鼠标过不去_灵耀X2 Duo双屏笔记本是怎样“炼”成的?对话华硕笔记本设计团队...
  8. 中学编程_您可以从30岁的第一次编程采访中学到什么
  9. sql limit 子句_SQL Join子句介绍和概述
  10. Ubuntu 为普通用户添加sudo权限
  11. 用正则表达式验证联系电话(及区号)
  12. IntelliJ IDEA的官方汉化插件下载
  13. Word 有时候百度输入法用不
  14. 技术状态管理(六)-技术状态审核
  15. LeetCode:Kth Smallest Element in a BST
  16. opencv 实现照片美颜功能 html5版(源码)
  17. python教程菜鸟教程学习路线
  18. matlab中复合中点式程序,《现代数值计算》Matlab程序整理(23页)-原创力文档
  19. 【GDSOI2017】魔兽争霸 x
  20. QEMU 模拟器(一)

热门文章

  1. 关于Cat,同轴,光纤等以太网电缆的所有信息
  2. 小白也能看懂的零知识证明与zk-SNARKs
  3. 优化算法(四)——粒子群优化算法(PSO)
  4. Linux中的yum是什么?如何配置?如何使用?
  5. C++定时切换桌面背景
  6. 鲲鹏Devkit代码迁移工具课堂总结
  7. 【Unreal4】gitignore目录减小项目大小
  8. linux-- input子系统分析
  9. isis宣告网络_ISIS是一个分级的链接状态路由协议
  10. Gym - 100502G Outing (强连通缩点+树形依赖背包)