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

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

问题描述

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

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

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

此测试遍历 x 千个文本,在这种情况下,我不希望每次失败都停止它.我希望对错误进行缓冲,以防错误在最后失败.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 允许 参数化 测试,方法是使用 <代码>参数化测试运行器.这样做可以确保每个测试用例都成为一个唯一的实例,使测试报告清楚地说明哪些样本通过了,哪些样本失败了.

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 的 Soft Assertions 允许吞下"单个断言失败,累积它们并仅在测试完成后报告.链接文档部分使用了一个很好的例子,绝对值得一读.

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天全站免登陆