JUnit故障回调/方法

Ben*_*nny 7 junit unit-testing annotations callback handler

是否有可能在测试用例或断言失败时触发方法,以便在测试用例失败时执行某些操作(例如,在UI测试时屏幕截图,编写错误日志等等).

也许有类似注释的东西,我还没有注意到.

提前致谢!

kry*_*ger 7

您可以使用该TestWatcher规则并实现自己的failed方法来截取测试失败时的屏幕截图或任何您需要做的事情.官方文档中略有修改的示例:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)