JUnit的失败回调/法 [英] JUnit on failure callback/method
本文介绍了JUnit的失败回调/法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能触发每当一个测试用例或断言失败的方法,为了做一些事情,当一个测试用例失败(如截图,同时UI测试,写一个错误日志,等等...)。
Is there any possibility to trigger a method whenever a testcase or assertion fails, in order to do some things when a testcase fails (e.g. Screenshot while UI-Testing, writing an error log, and so on...).
也许有类似的注解,我还没有注意到。
Maybe there is something like an annotation, I did not yet notice.
在此先感谢!
推荐答案
您可以使用 TestWatcher
规则并实现自己的失败
方法采取截图或任何你需要在测试失败的事情。稍微从官方文档变形例
You can use the TestWatcher
rule and implement your own failed
method to take a screenshot or whatever you need to do upon test failure. Slightly modified example from the official documentation:
public static class WatchmanTest {
private static String watchedLog;
@Rule
public MethodRule watchman = new TestWatcher() {
@Override
protected void failed(Throwable e, Description description) {
watchedLog += d + "\n";
// take screenshot, etc.
}
};
@Test
public void fails() {
fail();
}
}
这篇关于JUnit的失败回调/法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文