JUnit on failure callback/method

扶醉桌前 提交于 2019-12-08 19:14:06

问题


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!


回答1:


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();
    }
}


来源:https://stackoverflow.com/questions/19956955/junit-on-failure-callback-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!