PHPUnit

-------------------------------window :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-html Write agile documentation in HTML format to file.
–testdox-text Write agile documentation in Text format to file.
–log-tap Log test progress in TAP format to file.
–log-xml Log test progress in XML format to file.
–loader TestSuiteLoader 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( );
}
}
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值