調試程序是一個漫長的過程,程序越長越復雜,調試起來就愈加困難。如果你調試的是php程序,那么不妨采用phpUnit,它可以大大加快你的調試速度。 何謂PhpUnit Phpunit 脫胎于Fred Yankowski編寫的著名的Junit測試框架。你可以到它的網站 http://www.ontosys.com/phiki/phpunit 下載最新的版本。你可以利用phpUnit編寫一套測試軟件包。保證你的程序代碼正確無誤。只需一步便可自動完成所有的測試。 如果監測到bug,你就可以再寫一小段測試代碼來找出錯誤之所在。日后若再有相同的bug出現,只要運行你先前的測試包,馬上就可以抓到它。經常運行測試包便可以保證你的程序代碼的強壯性。 開 始 假設我們有一個銀行賬務處理程序。現在需要為Account (賬戶) 類編寫一個測試軟件包。 以下是Account類 源代碼: <?php class Account{ var $balance; function Account($initialBalance=0){ $this->balance = $initialBalance; } function withdraw($amount){ $this->balance -= $amount; } function deposit($amount){ $this->balance += $amount; } function getBalance(){ return $this->balance; } function transferFrom(&$sourceAccount,$amount){ $sourceAccount->withdraw($amount); $this->deposit($amount); } ?> 創建一個測試類 首先,我們建立一個測試類AccountTest,它是一個由PhpUnit提供的TestCase的子類。在這個TestCase類中有2個基本的方法:setUp和tearDown。 這2個方法的實現在父類中是空過程,必須由我們自己去重載。其中SetUp 用于進行AccountTest類的初始化處理。在本例中,我們對一些在測試中用到的賬號進行初始化。tearDown 則用于AccountTest類的清空處理,在本例中無需使用。因此,就不對它進行重載。這樣AccountTester類的源代碼如下: <?php class AccountTester extends TestCase{ var $_ac1; var $_ac2; var $_ac3; var $_ac4; function AccountTester($name){ $this->TestCase($name); // call parent constructor } function setUp(){ $this->_ac1 = new Account(100); // data for testWithdraw $this->_ac2 = new Account(20); // data for testDeposit $this->_ac3 = new Account(30); // data for testTransferFrom $this->_ac4 = new Account(50); } } ?> 加入專門的測試代碼 現在,我們可以往向AccountTester類加入測試代碼了。 <?php // Make a withdrawal of 25 units from _ac1. // _ac1's initial balance is 100 function testWithdraw(){ $this->_ac1->withdraw(25); $this->assert($this->_ac1->getBalance() == 75); // 100 - 25 = 75 } // Make a deposit of 10 units into _ac2. // _ac1's initial balance is 20 function testDeposit(){ $this->_ac2->deposit(10); $this->assertEquals(30,$this->_ac2->getBalance()); //20 +10 = 30 } // Tranfers 10 units from _ac3 to _ac4 // _ac3's initial balance is 30 // _ac4's initial balance is 50 function testTransferFrom(){ $this->_ac4->transferFrom(&$this->_ac3,10); $this->assertEquals(20,$this->_ac3->getBalance(),"Source account balance incorrect"); // 30 - 10 = 20 $this->assertEquals(60,$this->_ac4->getBalance(),"Target account balance incorrect"); // 50 + 10 = 60 } ?> 這段代碼中,assert(如同C里的斷言)方法是測試的關鍵部分。如果在assert中的條件表達式為真,那么測試通過。否則返回錯誤。由于assert方法大都用于判斷兩個變量的值是否相等。因此,testclass類引入了assertEquals方法專門實現這個功能。AssertEquals方法中有3個參數,依次分別為:期望值,測試值,兩值不相等時返回的消息提示串。 運行測試過程 好了,現在可以運行一下我們編好的測試程序了。我們還必須建立一個runtest.php測試程序來運行所有的測試過程。 runtest.php源代碼如下: <?php $tSuite = new TestSuite(); //creation of the test suite object 創建測試套件對象 $tSuite->addtest(new AccountTester("testWithdraw")); //Add inidividual tests $tSuite->addtest(new AccountTester("testDeposit")); //加入專門測試方法。 $tSuite->addtest(new AccountTester("testTransferFrom")); $res = new TextTestResult(); //Creation of the Result 建立一個測試結果類 $tSuite->run(&$res); //Run of the test 運行測試 $res->report(); //Print results 輸出測試結果。 ?> 程序說明: 首先創建測試套件對象tSuite,然后逐一加入專門測試方法,addtest方法的參數是測試方法的 再創建測試報告對象,隨之運行測試。測試發現錯誤的結果由TestResult類捕捉,TestResult可以定制一套text/html的錯誤報告。如果有必要你也可以自己編寫輸出部分。測試結果封裝在TestResult類中,為了輸出測試結果。我們采用了phpUnit提供的另外一個類TextTestResult類,它可以輸出文本或超文本格式的報告。當然我們也可以自己定制一個新的TestResult的子類控制更復雜的輸出格式。 提示和技巧 1、在編寫一個新的測試套件完成之后,我們可以先引入一個小小的bug以證明測試套件可以正常運行。 比如,在本例account類中,我們故意引入一個有問題的函數。 <?php function withdraw($amount){ $this->balance -= $Amount; // 變量名大小寫錯誤,本意是調用$amount參數,結果引入一個新變量$Amount。 } ?> 好,現在讓我們運行測試套件,如果不出意外的話,我們將很快發現錯誤之處。 2.要指出的是,并非所有的方法都需要測試。你只需對相關的方法進行測試。 3.如果在開始編碼前就寫好測試代碼,會使你更進一步明白你的程序到底需要完成什么樣的任務。 現在,通過引入phpUnit的測試套件類,你可以發現找bug的時間縮短了,而你作為一個程序員的工作效率也提高了。 那么,盡情享受抓蟲子的樂趣吧。祝您好胃口。
|