phpunit2试用手记
最近接触了不少关于php面向对象编程的知识,渐渐开始用类来实现功能代码。
发现原来看过的phpunit正好是用于类的单元测试的,于是又重新找了关于phpunit的资料,并用phpunit做了一些测试应用。以下是试用手记。
1、phpunit2的相关资料
下载地址:下载
说明文档:PHPUnit Pocket Guide
2、phpunit2的安装和命令行的使用
phpunit2需要php5.1及以上版本的支持,下载phpunit包后,放在pear目录,并将文件夹改为phpunit2。
包的根目录下有两个分别用于linux和windows命令行的“pear-phpunit”文件,根据自己机器的实际情况来修改。
我的系统是xp,就把pear-phpunit.bat的最后一行改成了:
“C:/php5/php.exe” “C:/php5/PEAR/PHPUnit2/TextUI/TestRunner.php” %*,
并保存成phpunit.bat。
记得要把php.exe和phpunit.bat的路径放到系统的环境变量中,这样才可以在cmd下进行命令行的操作。
如果以上都配置完毕,则在cmd下运行phpunit,将会有以下提示:
PHPUnit 2.3.0 by Sebastian Bergmann.
Usage: phpunit [switches] UnitTest [UnitTest.php]
–testdox-htmlWrite agile documentation in HTML format to file.
–testdox-textWrite agile documentation in Text format to file.
–log-tapLog test progress in TAP format to file.
–log-xmlLog test progress in XML format to file.
–loaderTestSuiteLoader implementation to use.
–skeleton Generate skeleton UnitTest class for Unit in Unit.php.
–wait Waits for a keystroke after each test.
–help Prints this usage information.
–version Prints the version and exits.
3、第一个测试类
还是用经典的银行类来说一下吧
银行类代码:
< ?php
class BankAccount
{
private $balance = 0;
public function getBalance( ) {
return $this->balance;
}
public function setBalance($balance) {
if ($balance >= 0) {
$this->balance = $balance;
} else {
throw new InvalidArgumentException;
}
}
public function depositMoney($amount) {
if ($amount >= 0) {
$this->balance += $amount;
} else {
throw new InvalidArgumentException;
}
}
public function withdrawMoney($amount) {
if ($amount >= 0 && $this->balance >= $amount) {
$this->balance -= $amount;
} else {
throw new InvalidArgumentException;
}
}
}
?>
测试类:
< ?php
require_once 'PHPUnit2/Framework/TestCase.php';
require_once 'BankAccount.php';
class BankAccountTest extends PHPUnit2_Framework_TestCase {
private $ba;
protected function setUp( ) {
$this->ba = new BankAccount;
}
public function testBalanceIsInitiallyZero( ) {
$this->assertEquals(0, $this->ba->getBalance( ));
}
public function testBalanceCannotBecomeNegative( ) {
try {
$this->ba->withdrawMoney(1);
}
catch (Exception $e) {
return;
}
$this->fail( );
}
public function testBalanceCannotBecomeNegative2( ) {
try {
$this->ba->depositMoney(-1);
}
catch (Exception $e) {
return;
}
$this->fail( );
}
public function testBalanceCannotBecomeNegative3( ) {
try {
$this->ba->setBalance(-1);
}
catch (Exception $e) {
return;
}
$this->fail( );
}
}
?>
Hick:
正好公司有这需求,最近也想搞搞单元测试, 国外一大牛推荐 Simple Test for PHP,回头再来交流
18 04月 2006, 12:52 pmmillsguo:
最近在看测试驱动开发,想学习下PHPUNIT2,搜到你这儿,希望下次能看到你对PHPUNIT2更详细的介绍。:)
18 04月 2006, 3:35 pm匿名:
:smile::sad::surprised::shock::???::cool::lol::mad::razz::redface::mrgreen::neutral::arrow::idea::?::!::wink::roll::twisted::evil::cry::faint::zk::down::ill::affect::sweat::love::hard::vacuity::money::emtf:
14 10月 2006, 8:42 pm匿名:

14 10月 2006, 8:46 pmsam:
总的感觉比较乱,步骤不够详细
15 01月 2007, 3:39 pmsam:
后面的类都写了,如何测试?
15 01月 2007, 3:41 pmleeyupeng:
to sam:到那个类文件所在的目录,在命令行下运行“phpunit BankAccountTest”就可以了。另:现在phpunit已经出到3了,还整合了更强大的selenium~
15 01月 2007, 9:50 pm匿名:
:mr
green:

19 11月 2008, 5:21 pm