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-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 条评论

  1. Hick:

    正好公司有这需求,最近也想搞搞单元测试, 国外一大牛推荐 Simple Test for PHP,回头再来交流 :)

  2. millsguo:

    最近在看测试驱动开发,想学习下PHPUNIT2,搜到你这儿,希望下次能看到你对PHPUNIT2更详细的介绍。:)

  3. 匿名:

    :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:

  4. 匿名:

    :grin:

  5. sam:

    总的感觉比较乱,步骤不够详细

  6. sam:

    后面的类都写了,如何测试?

  7. leeyupeng:

    to sam:到那个类文件所在的目录,在命令行下运行“phpunit BankAccountTest”就可以了。另:现在phpunit已经出到3了,还整合了更强大的selenium~

  8. 匿名:

    :shock: :grin: :neutral: :mr :money: :emtf: :zk: :zk: :zk: :zk: :zk: :zk: :zk: :zk: :zk: :zk: :zk: :zk: :zk: :zk: green: :?: :arrow:

  9. 匿名:

    :emtf: :emtf: :emtf: :emtf: :emtf: :emtf: :emtf: :emtf: :emtf: :emtf:

  10. 匿名:

    :emtf: :sad: :emtf: :sad: :emtf: :sad: :emtf: :sad:

留下评论