我在我的 Selenium 框架中添加了一个方法,可以在测试结束时截取屏幕截图,但由于某些测试由于失败而无法一直运行到最后。我想实现一个规则,以便在测试失败时运行此屏幕截图方法。
类似于:
@Rule
public Failure fail = new Failure();
if (fail)
{
// Method
}
最佳答案
您可以使用TestWatcher
,它具有失败和成功结果的 Hook 。你可以添加你的钩子(Hook)逻辑
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
public class Test1 {
@Rule
public TestWatcher watch = new TestWatcher() {
@Override
protected void failed(Throwable e, Description description) {
System.out.println("failed");
}
};
@Test
public void test() {
Assert.assertEquals(2, 4);
}
}
关于java - 如果在 Junit 上测试失败则运行方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58755369/