不要让测试因失败而停止 [英] Don't let test stop on failure

查看:60
本文介绍了不要让测试因失败而停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找以下(简化)方案的最佳实践:

I'm looking for the best practice for following (simplified) scenario:

@Test
public void someTest() {
  for(String someText : someTexts) {
    Assert.true(checkForValidity(someText));
  }
}

此测试遍历数千个文本,在这种情况下,我不希望每次失败都停止它.我希望对错误进行缓冲,以防错误最终失败. JUnit有什么适合我的目标吗?

This test iterates through x-thousands of texts and in this case I don't want it to be stopped for each failure. I want the errors to be buffered and in case of error(s) to fail at the end. Has JUnit got something on board for for my aim?

推荐答案

首先,这并不是实现此目的的正确方法. JUnit通过使用Parametrized测试跑步者.这样可以确保每个测试用例都成为唯一的实例,从而使测试报告清楚地说明哪些样本通过了哪些样本,哪些样本没有通过.

First of all, it's not really the correct way to implement this. JUnit allows parametrizing tests by defining a collection of inputs/outputs with the Parametrized test runner. Doing it this way ensures that each test case becomes a unique instance, making test report clearly state which samples passed and which ones failed.

如果您仍然坚持以自己的方式进行操作,则应该看看AssertJ的软件断言,它允许吞并"单个断言失败,累积它们并仅在测试完成后才报告.链接的文档部分使用了一个很好的示例,绝对值得一读.

If you still insist on doing it your way you should have a look at AssertJ's Soft Assertions which allow "swallowing" individual assertion failures, accumulating them and only reporting after the test is finished. The linked documentation section uses a nice example and is definitely worth reading.

这篇关于不要让测试因失败而停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆