- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下 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 测试。
我希望在失败时重试。知道我怎样才能实现它吗?
编辑:为了更好地理解,您可以使用我的代码来举例说明要添加的内容。谢谢。欣赏它。
最佳答案
首先我同意GhostCat的观点。
Flakey 测试代码才是真正的问题。
但是,如果“不稳定”不在您的代码中(例如与外部 Web 服务的网络连接不良),那么重新运行测试可能会很有用。
在这种情况下,您可以执行以下操作。
首先创建一个接口(interface)注解。(这将用于指示哪些测试需要重试。)
@Retention(RetentionPolicy.RUNTIME)
public @interface Retry {}
然后将TestRule
应用于我们的测试。(如果存在 Retry
注释,此规则将检查失败)
public class RetryRule implements TestRule {
@Override
public Statement apply(Statement base, Description method) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
base.evaluate();
} catch (Throwable t) {
Retry retry = method.getAnnotation(Retry.class);
if (retry != null) {
base.evaluate();
} else {
throw t;
}
}
}
};
}
}
最后,在我们的测试中,我们将所有内容放在一起
public class RetryTest {
private static int count = 0;
@Rule
public RetryRule rule = new RetryRule();
@Test
@Retry
public void testToRetry() throws Exception {
callMyFlakeyCode();
}
}
关于java - 如果 JUnit 测试失败,如何在 SuiteClass 中重试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54883604/
SuiteClasses 可以很好地处理像 {Test1.class,Test2.class} 这样的类列表,但是当我尝试生成类的静态列表时,它说类型不兼容:需要 java.lang.Class但发现
目前我有以下代码来测试我的类(class)... @RunWith(Suite.class) @SuiteClasses( { MyClass.class, MyNewClass } ) public
我目前正在学习 JUnit。我遇到了两种将多个类传递给 runClasses 方法的方法,即:带有 @SuitClasses 注释和类数组。 @RunWith(Suite.class)
我正在使用 classpath suite library自动运行项目中的所有测试,效果很好。 @RunWith(ClasspathSuite.class) @SuiteTypes({RUN_WITH
我有以下 RetryRule 类: public class RetryRule implements TestRule { private int retryCount; publi
我在另一个包中有一个数组列表,如下所示: String [ ] classes= {"SuiteTest1", "SuiteTest2"} 如何获取这些类并设置@SuiteClasses({})? p
我正在尝试使用类别针对 AllTests 测试套件运行 JUnit4 测试。在示例 1 中,运行 Suite3 有效,而示例 2 运行 Suite2 产生以下异常。 java.lang.Excepti
我正在尝试迁移到 JUnit 4,但我不清楚设置测试套件的正确方法。 我知道如何使用 @SuitesClasses 注释设置带有固定测试的测试套件。 但是,我想要一个顶级套件类,我可以在其中以编程方式
我是一名优秀的程序员,十分优秀!