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