为了并行执行我的JUnit测试,我使用了here提供的解决方案(感谢@peanut& @Reid Mac)。这适用于JUnit 4,但我正在尝试从JUnit 4迁移到JUnit 5.由于JUnit 5测试由JUnitPlatform
执行,直接扩展Runner
,因此JUnit 5无法实现等价.JUnit 5有没有像setScheduler
这样的方法?以下是我的代码片段。
JUnit4Runner.class
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.runners.Suite;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;
import org.junit.runners.model.RunnerScheduler;
public final class JUnit4Runner extends Suite {
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
public @interface Concurrent {
int threads() default 1;
}
/**
* Used by JUnit Concurrent
* https://stackoverflow.com/questions/5674774/running-junit-test-in-parallel-on-suite-level
*/
public AJUnit4Runner2(Class<?> suiteClass, RunnerBuilder builder) throws InitializationError {
super(suiteClass, builder);
setScheduler(new RunnerScheduler() {
ExecutorService executorService = Executors.newFixedThreadPool(
suiteClass.isAnnotationPresent(Concurrent.class)
? suiteClass.getAnnotation(Concurrent.class).threads()
: 1,
new NamedThreadFactory(suiteClass.getSimpleName()));
CompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService);
Queue<Future<Void>> tasks = new LinkedList<Future<Void>>();
@Override
public void schedule(Runnable childStatement) {
tasks.offer(completionService.submit(childStatement, null));
}
@Override
public void finished() {
try {
while (!tasks.isEmpty()) {
tasks.remove(completionService.take());
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
while (!tasks.isEmpty()) {
tasks.poll().cancel(true);
}
executorService.shutdownNow();
}
}
});
}
class NamedThreadFactory implements ThreadFactory {
final AtomicInteger poolNumber = new AtomicInteger(1);
final AtomicInteger threadNumber = new AtomicInteger(1);
final ThreadGroup group;
public NamedThreadFactory(String poolName) {
group = new ThreadGroup(poolName + "-" + poolNumber.getAndIncrement());
}
@Override
public Thread newThread(Runnable r) {
return new Thread(group, r, group.getName() + "-thread-" + threadNumber.getAndIncrement(), 0);
}
}
}
JUnit5跑步者类看起来像这样
JUnit5Runner.class
public class JUnit5Runner extends JUnitPlatform {
public JUnit5Runner(Class<?> testClass) throws InitializationError {
super(testClass);
...
}
@Override
public void run(RunNotifier notifier) {
super.run(notifier);
}
}
答案 0 :(得分:3)
Junit 5.3 M1现在支持并行测试执行。
如documentation中所述,它目前是实验性功能。您可以通过在junit-platform.properties中将junit.jupiter.execution.parallel.enabled
配置参数设置为true来启用它。