我发现jUnit中的测试用例是按顺序执行的,如何让它们并行执行?
答案 0 :(得分:36)
Junit4使用 ParallelComputer :
提供并行功能public class ParallelComputerTest {
@Test
public void test() {
Class[] cls={ParallelTest1.class,ParallelTest2.class };
//Parallel among classes
JUnitCore.runClasses(ParallelComputer.classes(), cls);
//Parallel among methods in a class
JUnitCore.runClasses(ParallelComputer.methods(), cls);
//Parallel all methods in all classes
JUnitCore.runClasses(new ParallelComputer(true, true), cls);
}
public static class ParallelTest1 {
@Test public void a(){}
@Test public void b(){}
}
public static class ParallelTest2 {
@Test public void a(){}
@Test public void b(){}
}
}
答案 1 :(得分:-12)
以下是一些示例代码。这对我很有用。 ExecutorService的。
public class TestCases {
static ExecutorService exe ;
public static void main(String[] args) throws Throwable {
test1() ;
test2() ;
test3() ;
}
public static void test1() {
exe = Executors.newCachedThreadPool() ;
for (int i = 0 ; i < 10 ; i++) {
Test1 test1 = new Test1() ;
exe.execute(test1) ;
}
exe.shutdown() ;
while(!exe.isShutDown()) {
}
}
//same for test2 and test3
}
public class Test1 implements Runnable {
public Test1() {
}
@Test
public myTest throws Throwable {
}
}