如何在JUnit5中管理失败的测试 [英] How to manage failed tests in JUnit5

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

问题描述

我正在迁移到JUnit5,我想知道JUnit5中是否有一种简单的方法来知道测试何时失败或失败,就像我们在JUnit4中使用TestWatcher一样.

I'm migrating to JUnit5 and I'd like to know if there is an easy way in JUnit5 to know when a test failed or not, just like we had in JUnit4 with TestWatcher.

我做了一些研究,发现了类似但悬而未决的问题,例如: https://github.com/junit-team/junit5/issues/542 由于它们中的大多数都已经很老了,所以我想问的是是否有最新的解决方案.

I did some research and found similar, but open questions, like this one: https://github.com/junit-team/junit5/issues/542 Since most of them are quite old, I'm asking just in case there is a recent solution.

该想法是能够进行屏幕截图或将一些消息添加到我的日志中.因此,是否存在侦听器或扩展程序,或者可以快速使我管理Webdriver测试失败/通过/跳过的内容? 提前致谢!

The idea is to be able to do a screenshot or add some message into my log. So, is there a listener or extension or something that quickly allows me to manage the webdriver test's failure/pass/skip? Thanks in advance!

推荐答案

好的,谢谢glytching. 这比我预想的要难,但是终于做到了.正如您所指出的,我们需要创建一个实现BeforeTestExecutionCallbackAfterTestExecutionCallback的外部类(例如WatcherExtension),然后使用扩展名给出的上下文来评估异常. 在此类中,我们可以添加如下内容:

OK, thanks glytching. It was harder than I expected, but finally made it. As you pointed me, we need to create an external class (e.g. WatcherExtension) implementing BeforeTestExecutionCallback and AfterTestExecutionCallback and then evaluate the exceptions using the context given by the extension. In this class we can add something like this:

@Override
public void afterTestExecution(ExtensionContext extensionContext) throws Exception {
    Method testMethod = extensionContext.getRequiredTestMethod();
    Boolean testFailed = extensionContext.getExecutionException().isPresent();
    if (testFailed) {
        if (!TESTDATA.excluded.contains(testMethod.getName())) {
            takeScreenshot(testMethod);
            logErrorMessage("Test " + testMethod.getName() + " failed on " + getAppVersion() + " using " + TESTDATA.browser);
        } else {
            logInfoMessage("Test " + testMethod.getName() + " was skipped on " + getAppVersion() + " using " + TESTDATA.browser);
        }
    } else {
        logInfoMessage(testMethod.getName() + " was successfully executed on " + getAppVersion() + " using " + TESTDATA.browser);
    }

最后,您需要将此注释添加到测试类中:

Finally, you need to add this annotation to your test classes:

@ExtendWith(WatcherExtension.class)
public class MyTest extends TestFactory {
    @Test
    public void testMyTest() { //whatever  }
}

就我个人而言,我认为这是非常基本的东西,应该在以后的版本中由JUnit5直接实现. 谢谢!

Personally, I think this is something really basic and should be directly implemented by JUnit5 in future versions. Thanks!

这篇关于如何在JUnit5中管理失败的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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