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
规则并实现您自己的 failed
方法来截取屏幕截图或任何您想要的需要在测试失败时做.来自官方文档的略微修改示例::>
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 TestRule watchman = new TestWatcher() {
@Override
protected void failed(Throwable e, Description description) {
watchedLog += d + "\n";
// take screenshot, etc.
}
};
@Test
public void fails() {
fail();
}
}
这篇关于JUnit 失败回调/方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文