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

PHP
PHP 执行系统外部命令 system() exec() passthru()
最新的php 文件上传模型,支持多文件上传
php 静态页面中显示动态内容
数据库查询记录php 多行多列显示
谈PHP生成静态页面分析 模板+缓存+写文件
PHP 各种排序算法实现代码
PHP nl2br函数 将换行字符转成 <br>
php 分页原理详解
Discuz 模板语句分析及知识技巧
php win下Socket方式发邮件类
怎样去阅读一份php源代码
建站常用13种PHP开源CMS比较
php xml留言板 xml存储数据的简单例子
PHP 开源AJAX框架14种
PHP 替换模板变量实现步骤
PHP has encountered an Access Violation at 7C94BD02解决方法
php 正则匹配函数体
php 文件夹删除、php清除缓存程序
php download.php实现代码 跳转到下载文件(response.redirect)
PHP类(Class)入门教程

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 222 ::
收藏到网摘: 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测试程序来运行所有的测试过程。