PHPUnit 命令行測試執(zhí)行器可通過 ?phpunit?命令調(diào)用。下面的代碼展示了如何用 PHPUnit 命令行測試執(zhí)行器來運行測試:
$ phpunit ArrayTest
PHPUnit latest.0 by Sebastian Bergmann and contributors.
..
Time: 0 seconds
OK (2 tests, 2 assertions)
上面這個調(diào)用例子中,PHPUnit 命令行測試執(zhí)行器將在當(dāng)前工作目錄中尋找 ?ArrayTest.php? 源文件并加載之。而在此源文件中應(yīng)當(dāng)能找到 ?ArrayTest?測試用例類,此類中的測試將被執(zhí)行。對于每個測試的運行,PHPUnit 命令行工具輸出一個字符來指示進(jìn)展:
PHPUnit 區(qū)分失?。╢ailure)與錯誤(error)。失敗指的是被違背了的 PHPUnit 斷言,例如一個失敗的 ?assertSame()? 調(diào)用。錯誤指的是意料之外的異常(exception)或 PHP 錯誤。這種差異已被證明在某些時候是非常有用的,因為錯誤往往比失敗更容易修復(fù)。如果得到了一個非常長的問題列表,那么最好先對付錯誤,當(dāng)錯誤全部修復(fù)了之后再試一次瞧瞧還有沒有失敗。
讓我們來瞧瞧以下代碼中命令行測試執(zhí)行器的各種選項:
$ phpunit --help
PHPUnit latest.0 by Sebastian Bergmann and contributors.
Usage:
phpunit [options] 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
--coverage-cache <dir> Cache static analysis results
--warm-coverage-cache Warm static analysis cache
--coverage-filter <dir> Include <dir> in code coverage analysis
--path-coverage Perform path coverage analysis
--disable-coverage-ignore Disable annotations for ignoring code coverage
--no-coverage Ignore code coverage configuration
Logging Options:
--log-junit <file> Log test execution in JUnit XML format to file
--log-teamcity <file> Log test execution in TeamCity format to file
--testdox-html <file> Write agile documentation in HTML format to file
--testdox-text <file> Write agile documentation in Text format to file
--testdox-xml <file> Write agile documentation in XML format to file
--reverse-list Print defects in reverse order
--no-logging Ignore logging configuration
Test Selection Options:
--filter <pattern> Filter which tests to run
--testsuite <name> Filter which testsuite to run
--group <name> Only runs tests from the specified group(s)
--exclude-group <name> Exclude tests from the specified group(s)
--list-groups List available test groups
--list-suites List available test suites
--list-tests List available tests
--list-tests-xml <file> List available tests in XML format
--test-suffix <suffixes> Only search for test in files with specified suffix(es). Default: Test.php,.phpt
Test Execution Options:
--dont-report-useless-tests Do not report tests that do not test anything
--strict-coverage Be strict about @covers annotation usage
--strict-global-state Be strict about changes to global state
--disallow-test-output Be strict about output during tests
--disallow-resource-usage Be strict about resource usage during small tests
--enforce-time-limit Enforce time limit based on test size
--default-time-limit <sec> Timeout in seconds for tests without @small, @medium or @large
--disallow-todo-tests Disallow @todo-annotated tests
--process-isolation Run each test in a separate PHP process
--globals-backup 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-defect Stop execution upon first not-passed test
--stop-on-error Stop execution upon first error
--stop-on-failure Stop execution upon first error or failure
--stop-on-warning Stop execution upon first warning
--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
--fail-on-incomplete Treat incomplete tests as failures
--fail-on-risky Treat risky tests as failures
--fail-on-skipped Treat skipped tests as failures
--fail-on-warning Treat tests with warnings as failures
-v|--verbose Output more verbose information
--debug Display debugging information
--repeat <times> Runs the test(s) repeatedly
--teamcity Report test execution progress in TeamCity format
--testdox Report test execution progress in TestDox format
--testdox-group Only include tests from the specified group(s)
--testdox-exclude-group Exclude tests from the specified group(s)
--no-interaction Disable TestDox progress animation
--printer <printer> TestListener implementation to use
--order-by <order> Run tests in order: default|defects|duration|no-depends|random|reverse|size
--random-order-seed <N> Use a specific random seed <N> for random order
--cache-result Write test results to cache file
--do-not-cache-result Do not write test results to cache file
Configuration Options:
--prepend <file> A PHP script that is included as early as possible
--bootstrap <file> A PHP script that is included before the tests run
-c|--configuration <file> Read configuration from XML file
--no-configuration Ignore default configuration file (phpunit.xml)
--extensions <extensions> A comma separated list of PHPUnit extensions to load
--no-extensions Do not load PHPUnit extensions
--include-path <path(s)> Prepend PHP's include_path with given path(s)
-d <key[=value]> Sets a php.ini value
--cache-result-file <file> Specify result cache path and filename
--generate-configuration Generate configuration file with suggested settings
--migrate-configuration Migrate configuration file to current format
Miscellaneous Options:
-h|--help Prints this usage information
--version Prints the version and exits
--atleast-version <min> Checks that version is greater than min and exits
--check-version Check whether PHPUnit is the latest version
?phpunit UnitTest
?
UnitTest.php
? 源文件中聲明。UnitTest
?這個類必須滿足以下二個條件之一:要么它繼承自 ?PHPUnit\Framework\TestCase
?;要么它提供 ?public static suite()
? 方法,這個方法返回一個?PHPUnit\Framework\Test
? 對象,比如,一個 ?PHPUnit\Framework\TestSuite
? 類的實例。?phpunit UnitTest UnitTest.php
?
UnitTest
類提供的測試。這個類應(yīng)當(dāng)在指定的源文件中聲明。?--coverage-clover
?
?--coverage-crap4j
?
?--coverage-html
?
?--coverage-php
?
PHP_CodeCoverage
?對象,此對象含有代碼覆蓋率信息。?--coverage-text
?
?--log-junit
?
?--testdox-html
? 和 ?--testdox-text
?
?--filter
?
測試名稱將以以下格式之一進(jìn)行匹配:
?TestNamespace\TestCaseClass::testMethod
?
默認(rèn)的測試名稱格式等價于在測試方法內(nèi)使用 ?__METHOD__
? 魔術(shù)常量。
?TestNamespace\TestCaseClass::testMethod with data set #0
?
當(dāng)測試擁有數(shù)據(jù)供給器時,數(shù)據(jù)的每輪迭代都會將其當(dāng)前索引附加在默認(rèn)測試名稱結(jié)尾處。
?TestNamespace\TestCaseClass::testMethod with data set "my named data"
?
當(dāng)測試擁有使用命名數(shù)據(jù)集的數(shù)據(jù)供給器時,數(shù)據(jù)的每輪迭代都會將當(dāng)前名稱附加在默認(rèn)測試名稱結(jié)尾處。命名數(shù)據(jù)集的例子參見示例 3.1。
示例 3.1 命名數(shù)據(jù)集
<?php
use PHPUnit\Framework\TestCase;
namespace TestNamespace;
class TestCaseClass extends TestCase
{
/**
* @dataProvider provider
*/
public function testMethod($data)
{
$this->assertTrue($data);
}
public function provider()
{
return [
'my named data' => [true],
'my data' => [true]
];
}
}
?/path/to/my/test.phpt
?
有效的過濾器模式例子參見示例 3.2。
示例 3.2 過濾器模式示例
--filter 'TestNamespace\\TestCaseClass::testMethod'
--filter 'TestNamespace\\TestCaseClass'
--filter TestNamespace
--filter TestCaseClase
--filter testMethod
--filter '/::testMethod .*"my named data"/'
--filter '/::testMethod .*#5$/'
--filter '/::testMethod .*#(5|6|7)$/'
在匹配數(shù)據(jù)供給器時有一些額外的快捷方式,參見示例 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
? 標(biāo)注為測試標(biāo)記其所屬的分組。@author
? 和 ?@ticket
? 標(biāo)注都是 ?@group
? 的別名,分別允許基于作者和事務(wù) ID 篩選測試。?--exclude-group
?
@group
? 標(biāo)注為測試標(biāo)記其所屬的分組。?--list-groups
?
?--test-suffix
?
?--dont-report-useless-tests
?
?--strict-coverage
?
?--strict-global-state
?
?--disallow-test-output
?
?--disallow-todo-tests
?
@todo
? 標(biāo)注的測試。?--enforce-time-limit
?
?--process-isolation
?
?--no-globals-backup
?
$GLOBALS
?。?--static-backup
?
?--colors
?
使用彩色輸出。在 Windows 上,用 ANSICON 或 ConEmu。
本選項有三個可能的值:
never
?:完全不使用彩色輸出。當(dāng)未使用 ?--colors
? 選項時,這是默認(rèn)值。auto
?:如果當(dāng)前終端不支持彩色、或者輸出被管道輸出至其他命令、或輸出被重定向至文件時,不使用彩色輸出,其余情況使用彩色。always
?:總是使用彩色輸出,即使當(dāng)前終端不支持彩色、輸出被管道輸出至其他命令、或輸出被重定向至文件。當(dāng)使用了 ?--colors
? 選項但未指定任何值時,將選擇 ?auto
? 做為其值。
?--columns
?
max
?,則使用當(dāng)前終端所支持的最大列數(shù)。?--stderr
?
STDERR
?而非 ?STDOUT
?。?--stop-on-error
?
?--stop-on-failure
?
?--stop-on-risky
?
?--stop-on-skipped
?
?--stop-on-incomplete
?
?--verbose
?
--debug
?--loader
?
PHPUnit\Runner\TestSuiteLoader
? 實現(xiàn)。include_path
?配置指令中指定的每個目錄內(nèi)查找源文件。諸如 ?Project_Package_Class
?這樣的類名對應(yīng)的源文件名為 ?Project/Package/Class.php
??--repeat
?
--testdox
?--printer
?
PHPUnit\Util\Printer
? 并且實現(xiàn) ?PHPUnit\Framework\TestListener
? 接口。--bootstrap
?--configuration
?、?-c
?
phpunit.xml
? 或 ?phpunit.xml.dist
?(按此順序)存在于當(dāng)前工作目錄并且未使用 ?--configuration
?,將自動從此文件中讀取配置。phpunit.xml
? 或 ?phpunit.xml.dist
?(按此順序)將自動從此文件中讀取配置。?--no-configuration
?
phpunit.xml
? 與 ?phpunit.xml.dist
?--include-path
?include_path
?開頭添加指定路徑(可以多個)。-d
?請注意,選項不能放在參數(shù)之后。
更多建議: