問題描述
我有一堆在 JUnit 測試套件中組織的測試.這些測試極大地利用 selenium 來測試 Web 應用程序.因此,對于 selenium,這些測試的運行時間很長.由于測試數據庫中有一些重疊,套件中的測試類無法并行運行,我想并行運行這些套件.
I have a bunch of tests that are organized in JUnit test suites. These tests are greatly utilizing selenium to test a web application. So, naturaly for selenium, the runtime of these tests is quite long. Since the test classes in the suites can not run parallel due some overlaps in the test database, i would like to run the suites parallel.
JUnit ParallelComputer 只能在類或方法級別并行執行測試,JUnit 是否有任何標準方法可以通過套件執行此操作?
The JUnit ParallelComputer can only execute tests on class or method level in parallel, are there any standard ways for JUnit to do that with suites?
如果我只是將套件類傳遞給 junit 運行器并將計算機配置為在類級別并行化,它會選擇測試類本身,而不是套件.
If i just pass suite classes to the junit runner and configure the computer to parallelize on class level, it picks the test classes itself, not the suites.
br弗蘭克
推薦答案
這里有一些對我有用的代碼.我沒有寫這個.如果您使用 @RunWith(ConcurrentSuite.class)
而不是 @RunWith(Suite.class)
它應該可以工作.下面還需要一個注釋.
Here is some code that worked for me. I did not write this. If you use @RunWith(ConcurrentSuite.class)
instead of @RunWith(Suite.class)
it should work. There is an annotation that is also needed which is found below.
package utilities.runners;
import org.junit.internal.builders.AllDefaultPossibilitiesBuilder;
import org.junit.runner.Runner;
import org.junit.runners.Suite;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;
import org.junit.runners.model.RunnerScheduler;
import utilities.annotations.Concurrent;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
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;
/**
* @author Mathieu Carbou (mathieu.carbou@gmail.com)
*/
public final class ConcurrentSuite extends Suite {
public ConcurrentSuite(final Class<?> klass) throws InitializationError {
super(klass, new AllDefaultPossibilitiesBuilder(true) {
@Override
public Runner runnerForClass(Class<?> testClass) throws Throwable {
List<RunnerBuilder> builders = Arrays.asList(
new RunnerBuilder() {
@Override
public Runner runnerForClass(Class<?> testClass) throws Throwable {
Concurrent annotation = testClass.getAnnotation(Concurrent.class);
if (annotation != null)
return new ConcurrentJunitRunner(testClass);
return null;
}
},
ignoredBuilder(),
annotatedBuilder(),
suiteMethodBuilder(),
junit3Builder(),
junit4Builder());
for (RunnerBuilder each : builders) {
Runner runner = each.safeRunnerForClass(testClass);
if (runner != null)
return runner;
}
return null;
}
});
setScheduler(new RunnerScheduler() {
ExecutorService executorService = Executors.newFixedThreadPool(
klass.isAnnotationPresent(Concurrent.class) ?
klass.getAnnotation(Concurrent.class).threads() :
(int) (Runtime.getRuntime().availableProcessors() * 1.5),
new NamedThreadFactory(klass.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();
}
}
});
}
static final class NamedThreadFactory implements ThreadFactory {
static final AtomicInteger poolNumber = new AtomicInteger(1);
final AtomicInteger threadNumber = new AtomicInteger(1);
final ThreadGroup group;
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);
}
}
}
并且注解如下.
package utilities.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author Mathieu Carbou (mathieu.carbou@gmail.com)
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
public @interface Concurrent {
int threads() default 5;
}
這篇關于在套件級別上并行運行 JUnit 測試?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!