JUnit4 Tutorial

JUnit4比之前的版本可爱多了。

编写Testcase

使用JUnit4编写testcase不再有继承Testcase类的负担了。只要在测试方法加annotation @org.junit.Test就行了。

@org.junit.Test
public void test1(){
// ur test code here
}
一个类可以有多个这种加了@Test annotation标注的测试方法。它们会在跑testcase的时候无序无依赖的跑。

如果一个类中的Test方法需要做一些初始化和清理工作,就需要用到@BeforeClass, @AfterClass, @Before 和@After这几个标注了。

@BeforeClass
public void overallInitial(){
// This method will run ONCE and ONLY ONCE before all test methods in this class. Some initial works should be done here for all test methods in this class.
}

@Before
public void initalTestCase(){
// This method run every time before for a test method. Test method level initial work should be done here.
}

@After
public void clearupTestCase(){
// This method run every time after for a test method. Test method level clearup work should be done here.
}

@AfterClass
public void overallClearup(){
// This method will run ONCE and ONLY ONCE after all test methods in this class. Some clearup works should be done here for all test methods in this class.
}

编写testcase基本就这么简单。一切都用标注搞定就行了。

还有一些有用的标注:

忽略某个testcase
@Ignore
("This case is not supported yet")

1s内如果不能跑完就算失败
@Test
(timeout=1000)

如果抛出IOException则算测试成功
@Test
(expected=IOException.class)

组织和运行Testcase

编写完Testcase,一般需要将Testcase组织成Testsuite,这样可以一次跑多个Testcase类。JUnit4中组织Testcase的方式有多种。

通过Annotation

最简单的还是通过annotation。下面的类就是通过Annotation来将多个Testcase组织成一个Suite

package junit.testsuite;

import junit.testcase.JUnitTestCase;
import junit.testcase.TestCase2;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.
class)
@Suite.SuiteClasses({ JUnitTestCase.
class, TestCase2.class })
public class AllTestsUsingAnnotation {

}

上面的类不需要代码,就俩标注就行了。一个@org.junit.runner.RunWith,一个@org.junit.runners.Suite。@RunWith表示这个类将以哪种形式来跑。后面的类型必须是Runner接口的实现。在这里指定为Suite。@Suite.SuiteClasses则可以包含多个test unit类。

@Suite.SuiteClasses中的类也可以指定另一个TestSuite,这样就可以有多个包含层次了。不过其中的test unit不能间接或者直接的包含当前类,否则就死循环了嘛。

这个类在Eclipse里面是可以直接Run As JUnit Test的。

通过手工创建TestSuite

如果不使用标注,可以手动创建TestSuite。

package junit.testsuite;

import junit.framework.JUnit4TestAdapter;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.testcase.TestCase3;

public class AllTestsUsingSuiteMethod {

public static Test suite() {
TestSuite suite
= new TestSuite("Root Test");
// $JUnit-BEGIN$
suite.addTest(
new JUnit4TestAdapter(TestCase3.class));

suite.addTest(
new JUnit4TestAdapter(AllTestsUsingAnnotation.class));
// $JUnit-END$
return suite;
}

}

上面的类创建了一个TestSuite,同时向TestSuite里增加了两个test unit。其中第二个其实就是上面创建的一个TestSuite。

如果在一个TestSuite里面有重复的testcase,那么将只有一个会被运行,重复的将被忽略。

这个类在Eclipse里面是可以直接Run As JUnit Test的。

运行Testcase

除了能够在Eclipse里面运行之外,还可以在普通的程序甚至命令行中跑。

下面的code就是通过应用程序跑TestCase的。根据结果可以生成需要的表格等有组织的报表。

package junit.testsuite;

import junit.testcase.JUnitTestCase;
import junit.testcase.TestCase2;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class RunTestInMainApp {

public static void main(String[] args) {
Result result
= JUnitCore.runClasses(JUnitTestCase.class,
TestCase2.
class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
}

}

在命令行下也可以通过类似的方法跑。

java org.junit.runner.JUnitCore junit.testcase.JUnitTestCase, junit.testcase.TestCase2
posted @ 2011-03-01 23:57  深夜两点  阅读(3045)  评论(0编辑  收藏  举报