您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > PHPUnit
用phpunit調(diào)試PHP程序
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2014/1/3 11:37:10 ] 推薦標(biāo)簽:phpunit PHP

        調(diào)試程序是一個漫長的過程,程序越長越復(fù)雜,調(diào)試起來愈加困難。如果你調(diào)試的是php程序,那么不妨采用phpUnit,它可以大大加快你的調(diào)試速度。
何謂PhpUnit
        Phpunit 脫胎于Fred Yankowski編寫的的Junit測試框架。你可以到它的網(wǎng)站http://www.ontosys.com/phiki/phpunit下載新的版本。你可以利用phpUnit編寫一套測試軟件包。保證你的程序代碼正確無誤。只需一步便可自動完成所有的測試。
        如果監(jiān)測到bug,你可以再寫一小段測試代碼來找出錯誤之所在。日后若再有相同的bug出現(xiàn),只要運行你先前的測試包,馬上可以抓到它。經(jīng)常運行測試包便可以保證你的程序代碼的強壯性。
開 始
        假設(shè)我們有一個銀行賬務(wù)處理程序,F(xiàn)在需要為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);
}
?>
 
創(chuàng)建一個測試類
 
        首先,我們建立一個測試類AccountTest,它是一個由PhpUnit提供的TestCase的子類。在這個TestCase類中有2個基本的方法: setUp和tearDown。 這2個方法的實現(xiàn)在父類中是空過程,必須由我們自己去重載。其中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);
}
}
?>
 
加入專門的測試代碼

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd