[英]How to show all the failures in Junit Assertions
我的要求是在 Junit 测试运行后显示所有故障。
我尝试了两件事:
Assertions.assertEquals --> 这会在第一次失败后停止执行,在测试报告中我只看到第一次失败。
Assertions.assertEquals(expceted, actual,"Error Message");
assertj.SoftAssertions --> 这里发生了执行,但在测试报告中我没有看到任何失败消息。
SoftAssertions softAssertions = new SoftAssertions();
softAssertions.assertThat(expected).withFailMessage("Error Message", actual) ;
如果有任何其他类型的断言或我可以与这些断言一起使用的任何其他选项,有什么想法吗?
提前致谢 !!!
JUnit 5 添加了assertAll
:
assertAll(
() -> assertEquals(...),
() -> assertTrue(...)
// etc
);
它需要任意数量的 lambda,如果我没记错的话,每个 lambda 甚至可以抛出异常。 这意味着您可以委托给方法:
assertAll(
...
complexAssertion(...)
);
...
private void complexAssertion(...) throws IOException {
String expectedOutput = <read resource>;
SomeException thrown = assertThrows(SomeException.class, () -> ...);
assertInstanceOf(SomeOtherException.class, thrown.getCause());
// nested assertAll can also be used!
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.