当前位置: 首页 > 图文教程 > 网络编程 > PHP > 用phpUnit帮你调试php程序

PHP
php 远程图片保存到本地的函数类
php 破解防盗链图片函数
快速开发一个PHP扩展图文教程
PHP6 mysql连接方式说明
php 进度条实现代码
php discuz 主题表和回帖表的设计
php 无限级缓存的类的扩展
php adodb操作mysql数据库
php FPDF类库应用实现代码
sourcesafe管理phpproj文件的补充说明(downmoon)
AspNetAjaxPager,Asp.Net通用无刷新Ajax分页控件,支持多样式多数据绑定
php一句话cmdshell新型 (非一句话木马)
php 木马的分析(加密破解)
PHP 数组入门教程小结
php 方便水印和缩略图的图形类
PHP加速 eAccelerator配置和使用指南
PHP 组件化编程技巧
IIS6+PHP5+MySQL5+Zend Optimizer+phpMyAdmin安装配置图文教程 2009年
两个强悍的php 图像处理类1
PHP 数据库 常见问题小结

PHP 中的 用phpUnit帮你调试php程序


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 47 ::
收藏到网摘: n/a

调试程序是一个漫长的过程,程序越长越复杂,调试起来就愈加困难。如果你调试的是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测试程序来运行所有的测试过程。