PHPUnit
by admin on 十二月.13, 2010, under 未分類
PHPUnit 介紹
PHPUnit 其實是個 PHP 寫成的 Framework , 主要功能就是自動對我們所寫的PHP程式做輸入輸出的檢查
一個方便的工具來幫我們自動測試 , 而 PHPUnit 就可以讓我們自行撰寫測試的條件 , 當我們所寫的測試條件隨著時間累積的越多時 , 就可以避免一定程度的錯誤發生了
安裝 PHPUnit
可以參考 : http://www.phpunit.de/manual/3.6/en/installation.html
pear channel-discover pear.phpunit.de pear channel-discover components.ez.no pear channel-discover pear.symfony-project.com pear install phpunit/PHPUnit
範例:
<?php class StackTest extends PHPUnit_Framework_TestCase { public function testPushAndPop() { $stack = array(); $this->assertEquals(0, count($stack)); array_push($stack, 'foo'); $this->assertEquals('foo', $stack[count($stack)-1]); $this->assertEquals(1, count($stack)); $this->assertEquals('foo', array_pop($stack)); $this->assertEquals(0, count($stack)); } } ?>
範例與執行:
<?php class StackTest extends PHPUnit_Framework_TestCase { public function testEmpty() { $stack = array(); $this->assertEmpty($stack); return $stack; } /** * @depends testEmpty */ public function testPush(array $stack) { array_push($stack, 'foo'); $this->assertEquals('foo', $stack[count($stack)-1]); $this->assertNotEmpty($stack); return $stack; } /** * @depends testPush */ public function testPop(array $stack) { $this->assertEquals('foo', array_pop($stack)); $this->assertEmpty($stack); } } ?> <pre lang="txt"> phpunit --verbose DependencyFailureTest PHPUnit 3.6.0 by Sebastian Bergmann. DependencyFailureTest FS Time: 0 seconds There was 1 failure: 1) testOne(DependencyFailureTest) Failed asserting that <boolean:false> is true. /home/sb/DependencyFailureTest.php:6 There was 1 skipped test: 1) testTwo(DependencyFailureTest) This test depends on "DependencyFailureTest::testOne" to pass. FAILURES! Tests: 2, Assertions: 1, Failures: 1, Skipped: 1.
