1.被测试类
现有一个简单的类SampleUtil。
public class SampleUtil {
private static Random _random = new Random();
//声明一个随机长度的数组,并循环消费时间
public static void consumeMemory(int length) {
byte[] data = new byte[length];
for(int i = 0, j = 0; i < data.length; ++i) {
++j;
}
}
//提供一个睡眠时间,进行睡眠
public static void consumeTime(int time) {
ThreadUtil.sleepIgnoreInterruption(time);
}
public static void doSomething() {
consumeTime(Math.abs(_random.nextInt()) % 500);
consumeMemory(Math.abs(_random.nextInt()) % 100000);
}
}
2.基于junit的测试类
这是一个junit的测试类,提供了三个测试方法TestA,TestB,TestC,TestB和TestC的代码是相同的。
public class SimpleTestClass {
public void setUp() {
SampleUtil.doSomething();
}
public void tearDown() {
SampleUtil.doSomething();
}
public void testA() {
System.out.println("testA");
SampleUtil.doSomething();
}
public void testB() {
SampleUtil.doSomething();
}
public void testC() {
SampleUtil.doSomething();
}
}
3.punit的单线程测试
SoloRunner类是一个测试运行嚣,这个功能和junit是一致的。
import org.punit.runner.SoloRunner;
public class SimpleTestClass {
public static void main(String[] args) {
new SoloRunner().run(SimpleTestClass.class);
}
public void testA() {
SampleUtil.doSomething();
}
public void testB() {
SampleUtil.doSomething();
}
public void testC() {
SampleUtil.doSomething();
}
}
运行结果
[solo] Starting samples.SimpleTestClass
samples.SimpleTestClass
testA() - [369.527643ms]
testB() - [177.475674ms]
testC() - [31.667765ms]
total: 3, failures:0 (GREEN) - 649.119171ms
详见:SimpleTestClass.java
4.puint多执行器测试
public class ExecutorPoolSample {
public static void main(String[] args) {
SoloRunner runner = new SoloRunner();
runner.setExecutorPool(new ExecutorPoolImpl(2));//被始化两个执行器
runner.run(LongTimeExecutionPUnitTestSuite.class);
}
}
测试结果
[solo] Starting samples.LongTimeExecutionPUnitTestSuite
TestSuite: samples.LongTimeExecutionPUnitTestSuite
samples.LongTimeExecutionTest1
samples.LongTimeExecutionTest2
test1() - [4999.592478ms]
test1() - [4999.591929ms]
test2() - [4999.146197ms]
test2() - [4999.389538ms]
testA() - [4999.405991ms]
testA() - [4999.210059ms]
testB() - [4999.460004ms]
testB() - [4999.275771ms]
total: 8, failures:0 (GREEN) - 20073.888102ms
注意:一个test类只能由一个执行器来完成,如果二个测试类,初始化了三个执行器,效果与两个执行器相同
详见:ExecutorPoolSample.java
6.punit并发执行
多线程执行只需要把SoloRunner转换成ConcurrentRunner即可。
new SoloRunner().run(MyTest.class);
new ConcurrentRunner().run(MyTest.class);
默认ConcurrentRunner 建立10个线程执行test方法。可以通过构造函数ConcurrentRunner(int threadCount)设置并发线程数。
例:将SimpleTestClass.java修改为
new ConcurrentRunner(10).run(SimpleTestClass.class);
测试结果为
并发为10
[concurrent] Starting samples.SimpleTestClass
samples.SimpleTestClass
testA() - [483.032871ms]
testB() - [445.48479ms]
testC() - [474.085771ms]
total: 3, failures:0 (GREEN) - 1462.703498ms
并发为1
[concurrent] Starting samples.SimpleTestClass
samples.SimpleTestClass
testA() - [490.151636ms]
testB() - [87.513957ms]
testC() - [222.401633ms]
total: 3, failures:0 (GREEN) - 858.174093ms
非并发结果
[solo] Starting samples.SimpleTestClass
samples.SimpleTestClass
testA() - [369.527643ms]
testB() - [177.475674ms]
testC() - [31.667765ms]
total: 3, failures:0 (GREEN) - 649.119171ms
7.Junit4的测试
public class JUnitAnnotationSample extends TestCase {
public static void main(String[] args) {
SoloRunner runner = new SoloRunner();
runner.setConvention(new JUnitAnnotationConvention());
runner.run(JUnitAnnotationSample.class);
}
@BeforeClass
public static void init() {
System.out.println("beforeClass"); //$NON-NLS-1$
}
@AfterClass
public static void close() {
System.out.println("afterClass"); //$NON-NLS-1$
}
@Before
public void before() {
System.out.println("before"); //$NON-NLS-1$
}
@After
public void after() {
System.out.println("after"); //$NON-NLS-1$
}
public void test1() {
System.out.println("test1"); //$NON-NLS-1$
}
@Test(expected = NullPointerException.class)
public void test2() {
System.out.println("test2"); //$NON-NLS-1$
throw new NullPointerException();
}
@Test
public void test3() {
System.out.println("test3"); //$NON-NLS-1$
}
}
执行结果
[solo] Starting samples.JUnitAnnotationSample
samples.JUnitAnnotationSample
beforeClass
before
test1
after
test1() - [0.186185ms]
before
test2
after
test2() - [2.483775ms]
before
test3
after
test3() - [0.106903ms]
afterClass
total: 3, failures:0 (GREEN) - 144.273315ms
8.输出到pdf和image
public class RunnerSamples {
public static void main(String[] args) {
SoloRunner runner = new SoloRunner();
runner.addEventListener(new FileLogger());
runner.addEventListener(new OverviewReporter(new ImageRender()));
runner.addEventListener(new OverviewReporter(new PDFRender()));
runner.run(SimpleTestClass.class);
}
}
参考文献
1.punit官方网站.
阅读(1148) | 评论(0) | 转发(0) |