p-unit-0.12 版本支持运行JUnit 4.x的测试案例,无需改动任何测试代码! p-unit支持JUnit 4.x中的Annotation包括: @Test, @Before, @After, @BeforeClass, @AfterClass. p-unit本身也定义了5个名字相同的Annotation(在org.punit.annotation包下),还有一个独特的@Test(checkMethod)属性,在并发测试时非常有用。
下面是一个完整的demo代码, 注意只有main里的三行代码是p-unit的(main当然可以写在其他类里),其他的全部是测试案例本身:
import org.junit.*;
import org.junit.Test;
import org.punit.convention.*;
import org.punit.runner.*;
import junit.framework.*;
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");
}
@AfterClass
public static void close() {
System.out.println("afterClass");
}
@Before
public void before() {
System.out.println("before");
}
@After
public void after() {
System.out.println("after");
}
public void test1() {
System.out.println("test1");
}
@Test(expected = NullPointerException.class)
public void test2() {
System.out.println("test2");
throw new NullPointerException();
}
@Test
public void test3() {
System.out.println("test3");
}
}
p-unit -0.12 下载
p-unit 官方主页
下面是一个完整的demo代码, 注意只有main里的三行代码是p-unit的(main当然可以写在其他类里),其他的全部是测试案例本身:
import org.junit.*;
import org.junit.Test;
import org.punit.convention.*;
import org.punit.runner.*;
import junit.framework.*;
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");
}
@AfterClass
public static void close() {
System.out.println("afterClass");
}
@Before
public void before() {
System.out.println("before");
}
@After
public void after() {
System.out.println("after");
}
public void test1() {
System.out.println("test1");
}
@Test(expected = NullPointerException.class)
public void test2() {
System.out.println("test2");
throw new NullPointerException();
}
@Test
public void test3() {
System.out.println("test3");
}
}
p-unit -0.12 下载
p-unit 官方主页