【发布时间】:2019-07-19 21:13:08
【问题描述】:
我有以下 RetryRule 类:
public class RetryRule implements TestRule {
private int retryCount;
public RetryRule(int retryCount) {
this.retryCount = retryCount;
}
public Statement apply(Statement base, Description description) {
return statement(base, description);
}
private Statement statement(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Throwable caughtThrowable = null;
for (int i = 0; i < retryCount; i++) {
try {
base.evaluate();
return;
}
catch (Throwable t) {
caughtThrowable = t;
System.err.println(description.getDisplayName() + ": run " + (i + 1) + " failed.");
}
}
System.err.println(description.getDisplayName() + ": giving up after " + retryCount + " failures.");
if (caughtThrowable != null) {
throw caughtThrowable;
}
}
};
}
}
还有以下 SuiteClass:
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
MakeBookingTest.class,
PaymentTest.class
})
public class TestSuite {
}
这有两个测试类.. MakeBookingTest 和 PaymentTest。他们每个人都有多个 JUnit 测试。
我希望在它们失败时重试它们。知道如何实现吗?
编辑:为了更好地理解,您可以使用我的代码来举例说明要添加的内容。谢谢。欣赏它。
【问题讨论】:
-
不要误会我的意思……但是为什么?您希望从重新运行失败的测试中获得什么?
-
有时它会失败,因为它们可能很脆弱。但是 99% 的时间,如果我重新运行……它会起作用。午夜运行的詹金斯作业运行套件类。因此,理想情况下,它应该重试任何失败的测试。
标签: java unit-testing junit test-suite