#### PHPUnit是什么?
它是一款輕量級的php測試框架
#### 為什么要用PHPUnit?
1. facebook在用
2. 可以通過命令操控測試腳本
3. 可以測試性能
4. 可以測試代碼覆蓋率
5. 可以自動化的更新測試用例的參數數據
6. 各種格式的日志
6. 最最重要的是,功能強大,使用特別簡單
#### PHPUnit的安裝
~~~
pear channel-discover pear.phpunit.de
pear install phpunit/PHPUnit
~~~
#### 快速入門
~~~
<?php
require_once 'PHPUnit/Framework.php';
class ArrayTest extends PHPUnit_Framework_TestCase
{
public function testNewArrayIsEmpty()
{
// 創建數組fixture。
$fixture = array();
// 斷言數組fixture的尺寸是0。
$this->assertEquals(0, sizeof($fixture));
}
}
?>
~~~
1. ArrayTest為測試類
2. ArrayTest 繼承于PHPUnit_Framework_TestCase
3. 測試方法`testNewArrayIsEmpty()`,測試方法必須為`public`權限,一般以`test`開頭,或者你也可以選擇給其加注釋`@test`來表明該函數為測試函數
~~~
/**
* @test
*/
public function testNewArrayIsEmpty()
{
$fixture = array();
$this->assertEquals(0, sizeof($fixture));
}
~~~
#### 命令行啟動測試
phpunit 測試文件名,測試`ArrayTest.php`文件
~~~
phpunit ArrayTest
PHPUnit 3.2.10 by Sebastian Bergmann.
..
Time: 0 seconds
OK (2 tests)
~~~
#### 命令行參數
~~~
phpunit --help
PHPUnit 3.2.10 by Sebastian Bergmann.
Usage: phpunit [switches] UnitTest [UnitTest.php]
--log-graphviz <file> Log test execution in GraphViz markup.
--log-json <file> Log test execution in JSON format.
--log-tap <file> Log test execution in TAP format to file.
--log-xml <file> Log test execution in XML format to file.
--log-metrics <file> Write metrics report in XML format.
--log-pmd <file> Write violations report in PMD XML format.
--coverage-html <dir> Generate code coverage report in HTML format.
--coverage-xml <file> Write code coverage information in XML format.
--test-db-dsn <dsn> DSN for the test database.
--test-db-log-rev <r> Revision information for database logging.
--test-db-prefix ... Prefix that should be stripped from filenames.
--test-db-log-info ... Additional information for database logging.
--testdox-html <file> Write agile documentation in HTML format to file.
--testdox-text <file> Write agile documentation in Text format to file.
--filter <pattern> Filter which tests to run.
--group ... Only runs tests from the specified group(s).
--exclude-group ... Exclude tests from the specified group(s).
--loader <loader> TestSuiteLoader implementation to use.
--repeat <times> Runs the test(s) repeatedly.
--tap Report test execution progress in TAP format.
--testdox Report test execution progress in TestDox format.
--no-syntax-check Disable syntax check of test source files.
--stop-on-failure Stop execution upon first error or failure.
--verbose Output more verbose information.
--wait Waits for a keystroke after each test.
--skeleton Generate skeleton UnitTest class for Unit in Unit.php.
--help Prints this usage information.
--version Prints the version and exits.
--configuration <file> Read configuration from XML file.
-d key[=value] Sets a php.ini value.
~~~