【发布时间】:2011-07-28 14:19:12
【问题描述】:
【问题讨论】:
标签: junit
【问题讨论】:
标签: junit
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(){}
}
}
【讨论】:
runClasses的结果。
这里是一些示例代码。这对我来说真的很好。执行器服务。
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 {
}
}
【讨论】: