PHPUnit


-----------------------------window:phpunit 2のインストールとコマンドラインの使用phpunit 2にはphp 5が必要です.1以上のバージョンのサポートでは、phpunitパッケージをダウンロードしてpearディレクトリに置き、フォルダをphpunit 2に変更します.パッケージのルートディレクトリの下には、linuxとwindowsコマンドラインにそれぞれ使用される「pear-phpunit」ファイルが2つあり、自分のマシンの実際の状況に応じて変更されます.私のシステムはxpですpear-phpunit.batの最後の行は、「C:/php 5/php.exe」「C:/php 5/PEAR/HPUnit 2/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( ); } } ?>