JUnit:确保测试方法中的失败在 @After 方法中的失败之前显示

发布于 2024-07-11 13:28:44 字数 266 浏览 9 评论 0原文

我设计了一个小型测试用例类,旨在使模拟更容易(在本例中,使用 JUnit 4 和 EasyMock)。 其中一部分是在测试完成后验证模拟,因此模拟在用@After注释的方法中进行验证。

但是,如果测试方法本身存在故障,导致测试无法完成,mock验证失败,则JUnit报告的故障就是验证失败。 但是,如果报告的故障是测试本身的故障,则会更有用。

那么,JUnit 中有没有一种方法可以确保测试方法中的错误/失败始终优先于用 @After 注释的方法中出现的错误/失败显示?

I have designed a small test case class that is designed to make mocking easier (in this case, with JUnit 4 and EasyMock). Part of this is verifying the mocks after the test has finished, so the mocks are verified in a method annotated with @After.

However, if there is a failure in the test method itself, which causes the test not to be completed and the mock verification to fail, the failure reported by JUnit is the failure of verification. However, it would be more useful if the failure reported was the failure in the test itself.

So, is there a way in JUnit to make sure that errors/failures in the test method are always displayed in preference to errors/failures that arise in methods annotated with @After?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烙印 2024-07-18 13:28:44

一般来说,@After注释相当于tearDown(),它暗示它应该只进行测试清理,而不是运行测试或做出断言。

我会让你的测试用例在每个测试用例结束时调用你的“验证”方法,而不是让 JUnit 为你调用它

Generally, the @After annotation is the equivalent of tearDown() which carries the implication that it should only be doing test cleanup, not running tests or making assertions.

I would have your test cases call your "verify" method at the end of each test case instead of having JUnit call it for you

满地尘埃落定 2024-07-18 13:28:44

您在测试中做了什么断言 @After 中的验证正在被报告? 如果断言失败(或者调用了fail()方法),则会报告该错误,而after方法将不会报告。 测试方法是什么样的?

What asserts are you doing in your tests that the verify in @After is being reported? If there is an assertion failure (or the fail() method is called) then that will be reported, and the after method will not be reported. What does the test method look like?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文