[英]JUnit repeat annotation does not start test case
I have a @Repeat annotation that I use to repeatedly run JUnit tests. 我有一个@Repeat批注,可用于重复运行JUnit测试。 The code was taken from this blog post here and modified to run with JUnit 4.10.
该代码来自此博客文章,并经过修改可与JUnit 4.10一起运行。
Repeat.java 重复.java
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Repeat {
int value();
}
ExtendedRunner.java ExtendedRunner.java
public class ExtendedRunner extends BlockJUnit4ClassRunner {
public ExtendedRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
protected Description describeChild(FrameworkMethod method) {
if (method.getAnnotation(Repeat.class) != null &&
method.getAnnotation(Ignore.class) == null) {
return describeRepeatTest(method);
}
return super.describeChild(method);
}
private Description describeRepeatTest(FrameworkMethod method) {
int times = method.getAnnotation(Repeat.class).value();
Description description = Description.createSuiteDescription(
testName(method) + " [" + times + " times]",
method.getAnnotations());
for (int i = 1; i <= times; i++) {
Description d = Description.createSuiteDescription("[" + i + "] " + testName(method));
d.addChild(Description.createTestDescription(getTestClass().getJavaClass(), testName(method)));
description.addChild(d);
}
return description;
}
@Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
if (method.getAnnotation(Repeat.class) != null) {
if (method.getAnnotation(Ignore.class) == null) {
Description description = describeRepeatTest(method);
runRepeatedly(methodBlock(method), description, notifier);
}
return;
}
super.runChild(method, notifier);
}
private void runRepeatedly(Statement statement, Description description,
RunNotifier notifier) {
for (Description desc : description.getChildren()) {
runLeaf(statement, desc, notifier);
}
}
}
And finally, the test to run wat()
10 times: 最后,测试运行
wat()
10次:
@RunWith(ExtendedRunner.class)
public class RepeatTest {
private int counter = 0;
@Repeat(10)
@Test
public void wat() {
System.out.println(counter++);
}
}
When I run the test, I get the following in the event log. 运行测试时,事件日志中显示以下内容。
Failed to start: 9 passed, 1 not started
And this as well: 还有:
Interestingly, the test printed from 0 - 9, which makes me believe the tests actually ran 10 times. 有趣的是,测试从0到9进行打印,这使我相信测试实际上运行了10次。 Yet, I get the above event log.
但是,我得到了上面的事件日志。 Why does this happen?
为什么会这样?
I have posted a @Rule
on GitHub that repeats tests. 我在GitHub上发布了一个
@Rule
,可以重复测试。 This solution is more flexible than using a Runner
since it does not preclude the use of another Runner
. 该解决方案比使用
Runner
更灵活,因为它不排除使用其他Runner
。
RepeatFailingTest Rule RepeatFailingTest规则
@ThreadSafe
@ParametersAreNonnullByDefault
public class RepeatTest implements TestRule {
public static RepeatTest findRepeats() {
return new RepeatTest();
}
private RepeatTest() {
super();
}
@Override
public Statement apply(final Statement statement, Description description) {
return new StatementRepeater(statement, description);
}
private final class StatementRepeater extends Statement {
private final Statement statement;
private final int repeatCount;
private StatementRepeater(Statement statement, Description description) {
super();
this.statement = statement;
Repeat repeat = description.getAnnotation(Repeat.class);
this.repeatCount = getRepeatCount(repeat);
}
private final int getRepeatCount(@Nullable Repeat repeat) {
int count = 1;
if (repeat != null) {
count = repeat.count();
}
assertThat("Repeat count must be > 0", count,
OrderingComparison.greaterThan(0));
return count;
}
@Override
public void evaluate() throws Throwable {
for (int i = 0; i < repeatCount; i++) {
statement.evaluate();
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.