至少我有过,不仅仅是喝咖啡,甚至是吃中饭,睡午觉,等待JUnit运行结果,以前总是抱怨机器太慢,现在,似乎情况有些变化,看下面的一个例子,有2个测试案例类[1],每个类4个方法,每个方法休息5秒钟,那么做一个简单的算术题,需要多长时间?
2*4*5=40秒,完全正确,你可以去倒一杯水,然后回来看结果了!
我机器上JUnit的运行结果为:
Run as -> JUnit -
40268ms 会有其他不同结果吗?p-unit给你答案,下面这段代码是运行p-unit的main函数:
public static void main(String[] args) {
SoloRunner runner = new SoloRunner();
runner.setExecutorPool(new ExecutorPoolImpl(2));
runner.run(LongTimeExecutionPUnitTestSuite.class);
}
答案是什么?
[solo] Started running samples.LongTimeExecutionPUnitTestSuite
TestSuite: samples.LongTimeExecutionPUnitTestSuite
samples.LongTimeExecutionTest1
samples.LongTimeExecutionTest2
test1() - [5021.0ms]
test1() - [5021.0ms]
test2() - [5003.0ms]
test2() - [5022.0ms]
testA() - [5020.0ms]
testA() - [5000.0ms]
testB() - [5001.0ms]
testB() - [5033.0ms]
total: 8, failures:0 (GREEN) - 20360.0ms
每个方法还是休息了5秒钟, 结果是20秒,不是40秒。
下载
p-unit-0.10 0.10 release!
p-unit 主页:
http://p-unit.sourceforge.net [1] 测试案例类
LongTimeExecutionTest1.java/LongTimeExecution2.java:
public class LongTimeExecutionTest1(2) extends TestCase {
public void test1() throws Exception {
Thread.sleep(5000);
}
public void test2() throws Exception {
Thread.sleep(5000);
}
public void testA() throws Exception {
Thread.sleep(5000);
}
public void testB() throws Exception {
Thread.sleep(5000);
}
}