7

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.

Thanks in advance!

4

1 回答 1

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();
    }
}
于 2013-11-13T15:29:06.770 回答