回歸測試框架-JUnit
通過前面的介紹,我們對JUnit有了一個大概的輪廓。知道了它是干什么的,F(xiàn)在讓我們動手改寫上面的測試類testCar使其符合Junit的規(guī)范--能在JUnit中運行。
//執(zhí)行測試的類(JUnit版)
import junit.work.*;
public class testCar extends TestCase {
protected int expectedWheels;
protected Car myCar;
public testCar(String name) {
super(name);
}
protected void setUp() {
expectedWheels = 4;
myCar = new Car();
}
public static Test suite() {
/*
* the type safe way
*
TestSuite suite= new TestSuite();
suite.addTest(
new testCar("Car.getWheels") {
protected void runTest() { testGetWheels(); }
}
);
return suite;
*/
/*
* the dynamic way
*/
return new TestSuite(testCar.class);
}
public void testGetWheels() {
assertEquals(expectedWheels, myCar.getWheels());
}
}
改版后的testCar已經(jīng)面目全非。先讓我們了解這些改動都是什么含義,再看如何執(zhí)行這個測試。
1>import語句,引入JUnit的類。(沒問題吧)
2>繼承 TestCase ?梢詴簳r將一個TestCase看作是對某個類進(jìn)行測試的方法的集合。詳細(xì)介紹請參看JUnit資料
3>setUp()設(shè)定了進(jìn)行初始化的任務(wù)。我們以后會看到setUp會有特別的用處。
4>testGetWheeels()對預(yù)期的值和myCar.getWheels()返回的值進(jìn)行比較,并打印比較的結(jié)果。assertEquals是junit.work.Assert中所定義的方法,junit.work.TestCase繼承了junit.work.Assert。
5>suite()是一個很特殊的靜態(tài)方法。JUnit的TestRunner會調(diào)用suite方法來確定有多少個測試可以執(zhí)行。上面的例子顯示了兩種方法:靜態(tài)的方法是構(gòu)造一個內(nèi)部類,并利用構(gòu)造函數(shù)給該測試命名(test name, 如 Car.getWheels ),其覆蓋的runTest()方法,指明了該測試需要執(zhí)行那些方法--testGetWheels()。動態(tài)的方法是利用內(nèi)。╮eflection )來實現(xiàn)runTest(),找出需要執(zhí)行那些測試。此時測試的名字即是測試方法(test method,如testGetWheels)的名字。JUnit會自動找出并調(diào)用該類的測試方法。
6>將TestSuite看作是包裹測試的一個容器。如果將測試比作葉子節(jié)點的話,TestSuite是分支節(jié)點。實際上TestCase,TestSuite以及TestSuite組成了一個composite Pattern。 JUnit的文檔中有一篇專門講解如何使用Pattern構(gòu)造Junit框架。有興趣的朋友可以查看JUnit資料。
如何運行該測試呢?手工的方法是鍵入如下命令:
[Windows] d:>java junit.textui.TestRunner testCar
[Unix] % java junit.textui.TestRunner testCar