讓我們來瞧瞧以下代碼中命令行測試運行器的各種選項:
~~~
phpunit --help
PHPUnit 5.0.0 by Sebastian Bergmann and contributors.
Usage: phpunit [options] UnitTest [UnitTest.php]
phpunit [options] <directory>
Code Coverage Options:
--coverage-clover <file> Generate code coverage report in Clover XML format.
--coverage-crap4j <file> Generate code coverage report in Crap4J XML format.
--coverage-html <dir> Generate code coverage report in HTML format.
--coverage-php <file> Export PHP_CodeCoverage object to file.
--coverage-text=<file> Generate code coverage report in text format.
Default: Standard output.
--coverage-xml <dir> Generate code coverage report in PHPUnit XML format.
Logging Options:
--log-junit <file> Log test execution in JUnit XML format to file.
--log-tap <file> Log test execution in TAP format to file.
--log-json <file> Log test execution in JSON format.
--testdox-html <file> Write agile documentation in HTML format to file.
--testdox-text <file> Write agile documentation in Text format to file.
Test Selection Options:
--filter <pattern> Filter which tests to run.
--testsuite <pattern> Filter which testsuite to run.
--group ... Only runs tests from the specified group(s).
--exclude-group ... Exclude tests from the specified group(s).
--list-groups List available test groups.
--test-suffix ... Only search for test in files with specified
suffix(es). Default: Test.php,.phpt
Test Execution Options:
--report-useless-tests Be strict about tests that do not test anything.
--strict-coverage Be strict about unintentionally covered code.
--strict-global-state Be strict about changes to global state
--disallow-test-output Be strict about output during tests.
--enforce-time-limit Enforce time limit based on test size.
--disallow-todo-tests Disallow @todo-annotated tests.
--process-isolation Run each test in a separate PHP process.
--no-globals-backup Do not backup and restore $GLOBALS for each test.
--static-backup Backup and restore static attributes for each test.
--colors=<flag> Use colors in output ("never", "auto" or "always").
--columns <n> Number of columns to use for progress output.
--columns max Use maximum number of columns for progress output.
--stderr Write to STDERR instead of STDOUT.
--stop-on-error Stop execution upon first error.
--stop-on-failure Stop execution upon first error or failure.
--stop-on-risky Stop execution upon first risky test.
--stop-on-skipped Stop execution upon first skipped test.
--stop-on-incomplete Stop execution upon first incomplete test.
-v|--verbose Output more verbose information.
--debug Display debugging information during test execution.
--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.
--printer <printer> TestListener implementation to use.
Configuration Options:
--bootstrap <file> A "bootstrap" PHP file that is run before the tests.
-c|--configuration <file> Read configuration from XML file.
--no-configuration Ignore default configuration file (phpunit.xml).
--include-path <path(s)> Prepend PHP's include_path with given path(s).
-d key[=value] Sets a php.ini value.
Miscellaneous Options:
-h|--help Prints this usage information.
--version Prints the version and exits.
~~~
`phpunit UnitTest`
運行由?`UnitTest`?類提供的測試。這個類應當在?`UnitTest.php`?源文件中聲明。
`UnitTest`?這個類必須滿足以下二個條件之一:要么它繼承自?`PHPUnit_Framework_TestCase`;要么它提供?`public static suite()`?方法,這個方法返回一個?`PHPUnit_Framework_Test`?對象,比如,一個?`PHPUnit_Framework_TestSuite`?類的實例。
`phpunit UnitTest UnitTest.php`
運行由?`UnitTest`?類提供的測試。這個類應當在指定的源文件中聲明。
`--coverage-clover`
為運行的測試生成帶有代碼覆蓋率信息的 XML 格式的日志文件。更多細節請參見[第 13 章](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#logging "第?13?章?Logging (日志記錄)")。
請注意,此功能僅當安裝了 tokenizer 和 Xdebug 這兩個 PHP 擴展后才可用。
`--coverage-crap4j`
生成 Crap4j 格式的代碼覆蓋率報告。更多細節請參見[第 11 章](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#code-coverage-analysis "第?11?章?代碼覆蓋率分析")。
請注意,此功能僅當安裝了 tokenizer 和 Xdebug 這兩個 PHP 擴展后才可用。
`--coverage-html`
生成 HTML 格式的代碼覆蓋率報告。更多細節請參見?[第 11 章](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#code-coverage-analysis "第?11?章?代碼覆蓋率分析")。
請注意,此功能僅當安裝了 tokenizer 和 Xdebug 這兩個 PHP 擴展后才可用。
`--coverage-php`
生成一個序列化后的 PHP_CodeCoverage 對象,此對象含有代碼覆蓋率信息。
請注意,此功能僅當安裝了 tokenizer 和 Xdebug 這兩個 PHP 擴展后才可用。
`--coverage-text`
為運行的測試以人們可讀的格式生成帶有代碼覆蓋率信息的日志文件或命令行輸出。更多細節請參見?[第 13 章](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#logging "第?13?章?Logging (日志記錄)")。
請注意,此功能僅當安裝了 tokenizer 和 Xdebug 這兩個 PHP 擴展后才可用。
`--log-junit`
為運行的測試生成 JUnit XML 格式的日志文件。更多細節請參見?[第 13 章](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#logging "第?13?章?Logging (日志記錄)")。
`--log-tap`
為運行的測試生成?[Test Anything Protocol (TAP)](http://testanything.org/)?格式的日志文件。更多細節請參見[第 13 章](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#logging "第?13?章?Logging (日志記錄)")。
`--log-json`
生成?[JSON](http://www.json.org/)?格式的日志文件。更多細節請參見[第 13 章](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#logging "第?13?章?Logging (日志記錄)")。
`--testdox-html`?和?`--testdox-text`
為運行的測試以 HTML 或純文本格式生成敏捷文檔。更多細節請參見?[第 12 章](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#other-uses-for-tests "第?12?章?測試的其他用途")。
`--filter`
只運行名稱與給定模式匹配的測試。如果模式未閉合包裹于分隔符,PHPUnit 將用?`/`?分隔符對其進行閉合包裹。
測試名稱將以以下格式之一進行匹配:
~~~
TestNamespace\TestCaseClass::testMethod
~~~
默認的測試名稱格式等價于在測試方法內使用?`__METHOD__`?魔術常量。
~~~
TestNamespace\TestCaseClass::testMethod with data set #0
~~~
當測試擁有數據供給器時,數據的每輪迭代都會將其當前索引附加在默認測試名稱結尾處。
~~~
TestNamespace\TestCaseClass::testMethod with data set "my named data"
~~~
當測試擁有使用命名數據集的數據供給器時,數據的每輪迭代都會將當前名稱附加在默認測試名稱結尾處。命名數據集的例子參見[例 3.1](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#textui.examples.TestCaseClass.php "例 3.1: 命名數據集")。
**例 3.1: 命名數據集**
~~~
<?php
namespace TestNamespace;
class TestCaseClass extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider provider
*/
public function testMethod($data)
{
$this->assertTrue($data);
}
public function provider()
{
return array(
'my named data' => array(true),
'my data' => array(true)
);
}
}
?>
~~~
`/path/to/my/test.phpt`
對于 PHPT 測試,其測試名稱是文件系統路徑。
有效的過濾器模式例子參見[例 3.2](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#textui.examples.filter-patterns "例 3.2: 過濾器模式例子")。
**例 3.2: 過濾器模式例子**
* `--filter 'TestNamespace\\TestCaseClass::testMethod'`
* `--filter 'TestNamespace\\TestCaseClass'`
* `--filter TestNamespace`
* `--filter TestCaseClass`
* `--filter testMethod`
* `--filter '/::testMethod .*"my named data"/'`
* `--filter '/::testMethod .*#5$/'`
* `--filter '/::testMethod .*#(5|6|7)$/'`
在匹配數據供給器時有一些額外的快捷方式,參見[例 3.3](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#textui.examples.filter-shortcuts "例 3.3: 過濾器的快捷方式")。
**例 3.3: 過濾器的快捷方式**
* `--filter 'testMethod#2'`
* `--filter 'testMethod#2-4'`
* `--filter '#2'`
* `--filter '#2-4'`
* `--filter 'testMethod@my named data'`
* `--filter 'testMethod@my.*data'`
* `--filter '@my named data'`
* `--filter '@my.*data'`
`--testsuite`
只運行名稱與給定模式匹配的測試套件。
`--group`
只運行來自指定分組(可以多個)的測試。可以用?`@group`?標注為測試標記其所屬的分組。
`@author`?標注是?`@group`?的一個別名,允許按作者來篩選測試。
`--exclude-group`
排除來自指定分組(可以多個)的測試。可以用?`@group`?標注為測試標記其所屬的分組。
`--list-groups`
列出所有有效的測試分組。
`--test-suffix`
只查找文件名以指定后綴(可以多個)結尾的測試文件。
`--report-useless-tests`
更嚴格對待事實上不測試任何內容的測試。詳情參見?[第 6 章](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#risky-tests "第?6?章?有風險的測試")。
`--strict-coverage`
更嚴格對待意外的代碼覆蓋。詳情參見?[第 6 章](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#risky-tests "第?6?章?有風險的測試")。
`--strict-global-state`
更嚴格對待全局狀態篡改。詳情參見?[第 6 章](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#risky-tests "第?6?章?有風險的測試")。
`--disallow-test-output`
更嚴格對待測試執行期間產生的輸出。詳情參見[第 6 章](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#risky-tests "第?6?章?有風險的測試")。
`--disallow-todo-tests`
不執行文檔注釋塊中含有?`@todo`?標注的測試。
`--enforce-time-limit`
根據測試規模對其加上執行時長限制。詳情參見[第 6 章](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#risky-tests "第?6?章?有風險的測試")。
`--process-isolation`
每個測試都在獨立的PHP進程中運行。
`--no-globals-backup`
不要備份并還原 $GLOBALS。更多細節請參見[“全局狀態”一節](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#fixtures.global-state "全局狀態")。
`--static-backup`
備份并還原用戶定義的類中的靜態屬性。更多細節請參見[“全局狀態”一節](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#fixtures.global-state "全局狀態")。
`--colors`
使用彩色輸出。Windows下,用?[ANSICON](https://github.com/adoxa/ansicon)?或?[ConEmu](https://github.com/Maximus5/ConEmu)。
本選項有三個可能的值:
* `never`: 完全不使用彩色輸出。當未使用?`--colors`?選項時,這是默認值。
* `auto`: 如果當前終端不支持彩色、或者輸出被管道輸出至其他命令、或輸出被重定向至文件時,不使用彩色輸出,其余情況使用彩色。
* `always`: 總是使用彩色輸出,即使當前終端不支持彩色、輸出被管道輸出至其他命令、或輸出被重定向至文件。
當使用了?`--colors`?選項但未指定任何值時,將選擇?`auto`?做為其值。
`--columns`
定義輸出所使用的列數。如果將其值定義為?`max`,則使用當前終端所支持的最大列數。
`--stderr`
選擇輸出到?`STDERR`?而非?`STDOUT`.
`--stop-on-error`
首次錯誤出現后停止執行。
`--stop-on-failure`
首次錯誤或失敗出現后停止執行。
`--stop-on-risky`
首次碰到有風險的測試時停止執行。
`--stop-on-skipped`
首次碰到跳過的測試時停止執行。
`--stop-on-incomplete`
首次碰到不完整的測試時停止執行。
`--verbose`
輸出更詳盡的信息,例如不完整或者跳過的測試的名稱。
`--debug`
輸出調試信息,例如當一個測試開始執行時輸出其名稱。
`--loader`
指定要使用的?`PHPUnit_Runner_TestSuiteLoader`?實現。
標準的測試套件加載器將在當前工作目錄和 PHP 的?`include_path`?配置指令中指定的每個目錄內查找源文件。諸如?`Project_Package_Class`?這樣的類名對應的源文件名為?`Project/Package/Class.php`。
`--repeat`
將測試重復運行指定次數。
`--tap`
使用?[Test Anything Protocol (TAP)](http://testanything.org/)?報告測試進度。更多細節請參見?[第 13 章](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#logging "第?13?章?Logging (日志記錄)")。
`--testdox`
將測試進度以敏捷文檔方式報告。更多細節請參見?[第 12 章](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#other-uses-for-tests "第?12?章?測試的其他用途")。
`--printer`
指定要使用的結果輸出器(printer)。輸出器類必須擴展?`PHPUnit_Util_Printer`?并且實現?`PHPUnit_Framework_TestListener`?接口。
`--bootstrap`
在測試前先運行一個 "bootstrap" PHP 文件。
`--configuration`,?`-c`
從 XML 文件中讀取配置信息。更多細節請參見[附錄 C](https://phpunit.de/manual/current/zh_cn/phpunit-book.html#appendixes.configuration "附錄?C.?XML 配置文件")。
如果?`phpunit.xml`?或?`phpunit.xml.dist`?(按此順序)存在于當前工作目錄并且*未*使用?`--configuration`,將自動從此文件中讀取配置。
`--no-configuration`
忽略當前工作目錄下的?`phpunit.xml`?與?`phpunit.xml.dist`。
`--include-path`
向 PHP 的?`include_path`?開頭添加指定路徑(可以多個)。
`-d`
設置指定的 PHP 配置選項的值。
>[info] ### 注意
> 請注意,從 4.8 開始,選項不能放在參數之后。
- PHPUnit 手冊
- 1. 安裝 PHPUnit
- 需求
- PHP 檔案包 (PHAR)
- Composer
- 可選的組件包
- 2. 編寫 PHPUnit 測試
- 測試的依賴關系
- 數據供給器
- 對異常進行測試
- 對 PHP 錯誤進行測試
- 對輸出進行測試
- 錯誤相關信息的輸出
- 3. 命令行測試執行器
- 命令行選項
- 4. 基境(fixture)
- setUp() 多 tearDown() 少
- 變體
- 基境共享
- 全局狀態
- 5. 組織測試
- 用文件系統來編排測試套件
- 用 XML 配置來編排測試套件
- 6. 有風險的測試
- 無用測試
- 意外的代碼覆蓋
- 測試執行期間產生的輸出
- 測試執行時長的超時限制
- 全局狀態篡改
- 7. 未完成的測試與跳過的測試
- 未完成的測試
- 跳過測試
- 用 @requires 來跳過測試
- 8. 數據庫測試
- 數據庫測試所支持的供應商
- 數據庫測試的難點
- 數據庫測試的四個階段
- PHPUnit 數據庫測試用例的配置
- 理解 DataSet(數據集)和 DataTable(數據表)
- 數據庫連接 API
- 數據庫斷言 API
- 常見問題(FAQ)
- 9. 測試替身
- Stubs (樁件)
- 仿件對象(Mock Object)
- Prophecy
- 對特質(Trait)與抽象類進行模仿
- 對 Web 服務(Web Services)進行上樁或模仿
- 對文件系統進行模仿
- 10. 測試實踐
- 在開發過程中
- 在調試過程中
- 11. 代碼覆蓋率分析
- 用于代碼覆蓋率的軟件衡量標準
- 包含與排除文件
- 略過代碼塊
- 指明要覆蓋的方法
- 邊緣情況
- 12. 測試的其他用途
- 敏捷文檔
- 跨團隊測試
- 13. Logging (日志記錄)
- 測試結果 (XML)
- 測試結果 (TAP)
- 測試結果 (JSON)
- 代碼覆蓋率 (XML)
- 代碼覆蓋率 (TEXT)
- 14. 擴展 PHPUnit
- 從 PHPUnit_Framework_TestCase 派生子類
- 編寫自定義斷言
- 實現 PHPUnit_Framework_TestListener
- 從 PHPUnit_Extensions_TestDecorator 派生子類
- 實現 PHPUnit_Framework_Test
- A. 斷言
- assertArrayHasKey()
- assertClassHasAttribute()
- assertArraySubset()
- assertClassHasStaticAttribute()
- assertContains()
- assertContainsOnly()
- assertContainsOnlyInstancesOf()
- assertCount()
- assertEmpty()
- assertEqualXMLStructure()
- assertEquals()
- assertFalse()
- assertFileEquals()
- assertFileExists()
- assertGreaterThan()
- assertGreaterThanOrEqual()
- assertInfinite()
- assertInstanceOf()
- assertInternalType()
- assertJsonFileEqualsJsonFile()
- assertJsonStringEqualsJsonFile()
- assertJsonStringEqualsJsonString()
- assertLessThan()
- assertLessThanOrEqual()
- assertNan()
- assertNull()
- assertObjectHasAttribute()
- assertRegExp()
- assertStringMatchesFormat()
- assertStringMatchesFormatFile()
- assertSame()
- assertStringEndsWith()
- assertStringEqualsFile()
- assertStringStartsWith()
- assertThat()
- assertTrue()
- assertXmlFileEqualsXmlFile()
- assertXmlStringEqualsXmlFile()
- assertXmlStringEqualsXmlString()
- B. 標注
- @author
- @after
- @afterClass
- @backupGlobals
- @backupStaticAttributes
- @before
- @beforeClass
- @codeCoverageIgnore*
- @covers
- @coversDefaultClass
- @coversNothing
- @dataProvider
- @depends
- @expectedException
- @expectedExceptionCode
- @expectedExceptionMessage
- @expectedExceptionMessageRegExp
- @group
- @large
- @medium
- @preserveGlobalState
- @requires
- @runTestsInSeparateProcesses
- @runInSeparateProcess
- @small
- @test
- @testdox
- @ticket
- @uses
- C. XML 配置文件
- PHPUnit
- 測試套件
- 分組
- 為代碼覆蓋率包含或排除文件
- Logging (日志記錄)
- 測試監聽器
- 設定 PHP INI 設置、常量、全局變量
- 為 Selenium RC 配置瀏覽器
- D. 升級
- E. 索引
- F. 參考書目
- G. 版權