windows下安装pear及phpunit(注意配置好php命令行环境)

本文详细介绍了如何使用PHP的PHPUnit框架进行自动化单元测试,包括安装、配置和编写测试用例的过程,以及如何利用断言函数提高测试效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从一个案例开始
通常我们在测试我们的php程序时,会使用诸如print,echo等语句来输出一些结果,以查看我们的程序是否正常或者哪里出错.例如:

<?php   

$array = array();   

print sizeof($array);   

array_push($array, ‘newElement’);   

print sizeof($array);  

结果
0
1
这样我们就知道$array变量是我们预期的结果.现在我们希望能够直观给出测试的结果,而不是看到0, 1这样的结果后再去判断. 我们修改测试代码:

 

<?php   

$array = array();   

print (sizeof($array) == 0) ? "OK\n" : "Not Ok\n";   

array_push($array, ‘newElement’);   

print (sizeof($array) == 1) ? "OK\n" : "Not Ok\n"; 

结果
OK
OK

为了方便重用及修改我们将测试代码的断言部分置于函数中

 

<?php   

$array = array();   

assertTrue(sizeof($array) == 0);   

array_push($array, ‘newElement’);   

assertTrue(sizeof($array) == 1);   

  

function assertTrue($condition)   

{   

  if (!$condition)   

    throw new Exception(‘Assertion Failed!’);   

}  

 

如此我们就可以简单的实现测试的自动化了.
自动测试的目的是减少错误,使用自动测试你能够明显发现你的代码的错误的减少.还有一些其他的好处……PHPUnit的目标就是如此.那么我们开始我们的PHP Unit Test吧.

一.  安装PHPUnit需要使用Pear Installer安装, 加入phpunit频道到pear寻找频道

D:\wamp\bin\php\php5.2.6>pear channel-discover pear.phpunit.de
Adding Channel "pear.phpunit.de" succeeded
Discovery of channel "pear.phpunit.de" succeeded

安装PHPUnit
D:\wamp\bin\php\php5.2.6>pear install phpunit/PHPUnit
Did not download optional dependencies: pear/Image_GraphViz, pear/Log, use –all
deps to download automatically
phpunit/PHPUnit can optionally use package "pear/Image_GraphViz" (version >= 1.2
.1)
phpunit/PHPUnit can optionally use package "pear/Log"
phpunit/PHPUnit can optionally use PHP extension "pdo_sqlite"
phpunit/PHPUnit can optionally use PHP extension "xdebug" (version >= 2.0.0)
downloading PHPUnit-3.3.4.tgz …
Starting to download PHPUnit-3.3.4.tgz (268,962 bytes)
………………………………………………..done: 268,962 bytes
install ok: channel://pear.phpunit.de/PHPUnit-3.3.4

安装成功,在pear路径下即可看到PHPUnit文件夹,所有PHPUnit源码都在其中.

注:下面的是在失败的情况下【为什么失败的情况,install failed】

接着安装单元测试框架phpunit:

d:\wamp\bin\php\php5.2.5>pear channel-discover pear.phpunit.de
Adding Channel "pear.phpunit.de" succeeded
Discovery of channel "pear.phpunit.de" succeeded

d:\wamp\bin\php\php5.2.5>pear install phpunit/PHPUnit
Did not download optional dependencies: pear/Image_GraphViz, pear/Log, channel:/
/pear.symfony-project.com/YAML, use --alldeps to download automatically
phpunit/PHPUnit requires PEAR Installer (version >= 1.8.1), installed version is
1.6.1
phpunit/PHPUnit can optionally use package "pear/Image_GraphViz" (version >= 1.2
.1)
phpunit/PHPUnit can optionally use package "pear/Log"
phpunit/PHPUnit can optionally use package "channel://pear.symfony-project.com/Y
AML" (version >= 1.0.2)
phpunit/PHPUnit can optionally use PHP extension "pdo_sqlite"
phpunit/PHPUnit can optionally use PHP extension "soap"
phpunit/PHPUnit can optionally use PHP extension "xdebug" (version >= 2.0.5)
No valid packages found
install failed
(安装phpunit失败,此时通过pear upgrade pear更新pear后继续安装)

d:\wamp\bin\php\php5.2.5>pear upgrade pear
downloading PEAR-1.9.0.tgz ...
Starting to download PEAR-1.9.0.tgz (291,634 bytes)
......

d:\wamp\bin\php\php5.2.5>pear info pear
ABOUT PEAR.PHP.NET/PEAR-1.9.0
......

d:\wamp\bin\php\php5.2.5>pear channel-discover pear.phpunit.de
Channel "pear.phpunit.de" is already initialized

d:\wamp\bin\php\php5.2.5>pear install phpunit/PHPUnit
Unknown remote channel: pear.symfony-project.com
Did not download optional dependencies: pear/Image_GraphViz, pear/Log, channel:/
/pear.symfony-project.com/YAML, use --alldeps to download automatically
phpunit/PHPUnit can optionally use package "pear/Image_GraphViz" (version >= 1.2
.1)
phpunit/PHPUnit can optionally use package "pear/Log"
phpunit/PHPUnit can optionally use package "channel://pear.symfony-project.com/Y
AML" (version >= 1.0.2)
phpunit/PHPUnit can optionally use PHP extension "pdo_sqlite"
phpunit/PHPUnit can optionally use PHP extension "soap"
phpunit/PHPUnit can optionally use PHP extension "xdebug" (version >= 2.0.5)
downloading PHPUnit-3.4.13.tgz ...
Starting to download PHPUnit-3.4.13.tgz (254,903 bytes)
.....................................................done: 254,903 bytes
install ok: channel://pear.phpunit.de/PHPUnit-3.4.13

转载于:https://www.cnblogs.com/yuanwangwu/archive/2012/08/02/PEAR.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值