我做了以下自定义BlockJUnit4ClassRunner
public class RepeatEachTest extends BlockJUnit4ClassRunner {
private int repeats;
public RepeatEachTest(Class<?> klass) throws InitializationError {
super(klass);
Repeat r = klass.getAnnotation(Repeat.class);
if (r == null) {
throw new InitializationError("A @Repeat annonation must also be suplied to class, for example @Repeat(5) to repeat 5 times");
}
repeats = r.value();
}
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
for (int i = 0; i < repeats; i++) {
super.runChild(method, notifier);
}
}
@Override
public int testCount() {
return repeats * super.testCount();
}
}
和
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Repeat {
int value();
}
执行每次测试@Repeat.value()
次。
@RunWith(RepeatEachTest.class)
@Repeat(2)
public class RepeatEachTestTest {
@Test
public void first() {
System.out.println("ran first");
}
@Test
public void second() {
System.out.println("ran second");
}
}
看起来像
ran first
ran first
ran second
ran second
但现在我想实现第二个BlockJUnit4ClassRunner,它运行整个测试类 @Repeat.value()
次。来自该设置的运行看起来像
ran first
ran second
ran first
ran second
有什么想法吗?
答案 0 :(得分:1)
这取决于你想要什么。如果您希望多次调用@BeforeClass
和@AfterClass
方法和类规则,则可以覆盖classBlock()
:
protected Statement classBlock(final RunNotifier notifier) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
for (int i = 0; i < repeats; i++) {
super.classBlock(notifier).evaluate();
}
}
};
}
如果您希望调用@BeforeClass
和@AfterClass
方法和类规则,请覆盖childrenInvoker()
(代码类似)。
但请注意,这些中的任何一个都会导致多次通知侦听器测试已经开始并完成。在这种情况下,有些听众可能行为不正确。