从一个案例开始
通常我们在测试我们的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