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.
高級功能
你是否已經厭煩了在每一個測試方法命名前面加一個test,是否因爲只是調用的參數不同,卻要寫多個測試用例而糾結?我最喜歡的高級功能,現在隆重推薦給你,叫做框架生成器
<?php
class Calculator
{
public function add($a, $b)
{
return $a + $b;
}
}
?>
命令行啓動測試用例
phpunit --skeleton Calculator
PHPUnit 3.2.10 by Sebastian Bergmann.
Wrote test class skeleton for Calculator to CalculatorTest.php.
簡單麼?簡單,但是它其實沒有什麼意義,因爲沒有測試數據,怎樣加數據,哦哦哦,重頭戲來了
<?php
class Calculator
{
/**
* @assert (0, 0) == 0
* @assert (0, 1) == 1
* @assert (1, 0) == 1
* @assert (1, 1) == 2
*/
public function add($a, $b)
{
return $a + $b;
}
}
?>
原始類中的每個方法都進行@assert註解的檢測。這些被轉變爲測試代碼,像這樣
/**
* Generated from @assert (0, 0) == 0.
*/
public function testAdd() {
$o = new Calculator;
$this->assertEquals(0, $o->add(0, 0));
}
下面是運行生成的測試用例類的輸出。
phpunit CalculatorTest
PHPUnit 3.2.10 by Sebastian Bergmann.
....
Time: 0 seconds
OK (4 tests)