junit5 - 运行 并行测试(pom 中没有 forkCount)
junit5 - run tests in parallel (without forkCount in pom)
为了并行执行我的 JUnit 测试,我使用了 here (Thanks to @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
*
*/
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 runner class 看起来像这样
JUnit5Runner.class
public class JUnit5Runner extends JUnitPlatform {
public JUnit5Runner(Class<?> testClass) throws InitializationError {
super(testClass);
...
}
@Override
public void run(RunNotifier notifier) {
super.run(notifier);
}
}
Junit 5.3 M1 现在支持并行测试执行。
正如 documentation 中提到的,它目前是一项实验性功能。您可以通过在 junit-platform.properties.
中将 junit.jupiter.execution.parallel.enabled
配置参数设置为 true 来启用它
为了并行执行我的 JUnit 测试,我使用了 here (Thanks to @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
*
*/
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 runner class 看起来像这样
JUnit5Runner.class
public class JUnit5Runner extends JUnitPlatform {
public JUnit5Runner(Class<?> testClass) throws InitializationError {
super(testClass);
...
}
@Override
public void run(RunNotifier notifier) {
super.run(notifier);
}
}
Junit 5.3 M1 现在支持并行测试执行。 正如 documentation 中提到的,它目前是一项实验性功能。您可以通过在 junit-platform.properties.
中将junit.jupiter.execution.parallel.enabled
配置参数设置为 true 来启用它