命令行選項(xiàng)

2018-02-24 15:41 更新

讓我們來瞧瞧以下代碼中命令行測試運(yùn)行器的各種選項(xiàng):

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

運(yùn)行由?UnitTest?類提供的測試。這個(gè)類應(yīng)當(dāng)在?UnitTest.php?源文件中聲明。

UnitTest?這個(gè)類必須滿足以下二個(gè)條件之一:要么它繼承自?PHPUnit_Framework_TestCase;要么它提供?public static suite()?方法,這個(gè)方法返回一個(gè)?PHPUnit_Framework_Test?對象,比如,一個(gè)?PHPUnit_Framework_TestSuite?類的實(shí)例。

phpunit UnitTest UnitTest.php

運(yùn)行由?UnitTest?類提供的測試。這個(gè)類應(yīng)當(dāng)在指定的源文件中聲明。

--coverage-clover

為運(yùn)行的測試生成帶有代碼覆蓋率信息的 XML 格式的日志文件。更多細(xì)節(jié)請參見第 13 章。

請注意,此功能僅當(dāng)安裝了 tokenizer 和 Xdebug 這兩個(gè) PHP 擴(kuò)展后才可用。

--coverage-crap4j

生成 Crap4j 格式的代碼覆蓋率報(bào)告。更多細(xì)節(jié)請參見第 11 章。

請注意,此功能僅當(dāng)安裝了 tokenizer 和 Xdebug 這兩個(gè) PHP 擴(kuò)展后才可用。

--coverage-html

生成 HTML 格式的代碼覆蓋率報(bào)告。更多細(xì)節(jié)請參見?第 11 章。

請注意,此功能僅當(dāng)安裝了 tokenizer 和 Xdebug 這兩個(gè) PHP 擴(kuò)展后才可用。

--coverage-php

生成一個(gè)序列化后的 PHP_CodeCoverage 對象,此對象含有代碼覆蓋率信息。

請注意,此功能僅當(dāng)安裝了 tokenizer 和 Xdebug 這兩個(gè) PHP 擴(kuò)展后才可用。

--coverage-text

為運(yùn)行的測試以人們可讀的格式生成帶有代碼覆蓋率信息的日志文件或命令行輸出。更多細(xì)節(jié)請參見?第 13 章。

請注意,此功能僅當(dāng)安裝了 tokenizer 和 Xdebug 這兩個(gè) PHP 擴(kuò)展后才可用。

--log-junit

為運(yùn)行的測試生成 JUnit XML 格式的日志文件。更多細(xì)節(jié)請參見?第 13 章。

--log-tap

為運(yùn)行的測試生成?Test Anything Protocol (TAP)?格式的日志文件。更多細(xì)節(jié)請參見第 13 章。

--log-json

生成?JSON?格式的日志文件。更多細(xì)節(jié)請參見第 13 章。

--testdox-html?和?--testdox-text

為運(yùn)行的測試以 HTML 或純文本格式生成敏捷文檔。更多細(xì)節(jié)請參見?第 12 章。

--filter

只運(yùn)行名稱與給定模式匹配的測試。如果模式未閉合包裹于分隔符,PHPUnit 將用?/?分隔符對其進(jìn)行閉合包裹。

測試名稱將以以下格式之一進(jìn)行匹配:

TestNamespace\TestCaseClass::testMethod

默認(rèn)的測試名稱格式等價(jià)于在測試方法內(nèi)使用?__METHOD__?魔術(shù)常量。

TestNamespace\TestCaseClass::testMethod with data set #0

當(dāng)測試擁有數(shù)據(jù)供給器時(shí),數(shù)據(jù)的每輪迭代都會將其當(dāng)前索引附加在默認(rèn)測試名稱結(jié)尾處。

TestNamespace\TestCaseClass::testMethod with data set "my named data"

當(dāng)測試擁有使用命名數(shù)據(jù)集的數(shù)據(jù)供給器時(shí),數(shù)據(jù)的每輪迭代都會將當(dāng)前名稱附加在默認(rèn)測試名稱結(jié)尾處。命名數(shù)據(jù)集的例子參見例 3.1。

例 3.1: 命名數(shù)據(jù)集

<?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 測試,其測試名稱是文件系統(tǒng)路徑。

有效的過濾器模式例子參見例 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)$/'

在匹配數(shù)據(jù)供給器時(shí)有一些額外的快捷方式,參見例 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

只運(yùn)行名稱與給定模式匹配的測試套件。

--group

只運(yùn)行來自指定分組(可以多個(gè))的測試??梢杂?@group?標(biāo)注為測試標(biāo)記其所屬的分組。

@author?標(biāo)注是?@group?的一個(gè)別名,允許按作者來篩選測試。

--exclude-group

排除來自指定分組(可以多個(gè))的測試??梢杂?@group?標(biāo)注為測試標(biāo)記其所屬的分組。

--list-groups

列出所有有效的測試分組。

--test-suffix

只查找文件名以指定后綴(可以多個(gè))結(jié)尾的測試文件。

--report-useless-tests

更嚴(yán)格對待事實(shí)上不測試任何內(nèi)容的測試。詳情參見?第 6 章。

--strict-coverage

更嚴(yán)格對待意外的代碼覆蓋。詳情參見?第 6 章。

--strict-global-state

更嚴(yán)格對待全局狀態(tài)篡改。詳情參見?第 6 章。

--disallow-test-output

更嚴(yán)格對待測試執(zhí)行期間產(chǎn)生的輸出。詳情參見第 6 章

--disallow-todo-tests

不執(zhí)行文檔注釋塊中含有?@todo?標(biāo)注的測試。

--enforce-time-limit

根據(jù)測試規(guī)模對其加上執(zhí)行時(shí)長限制。詳情參見第 6 章。

--process-isolation

每個(gè)測試都在獨(dú)立的PHP進(jìn)程中運(yùn)行。

--no-globals-backup

不要備份并還原 $GLOBALS。更多細(xì)節(jié)請參見“全局狀態(tài)”一節(jié)。

--static-backup

備份并還原用戶定義的類中的靜態(tài)屬性。更多細(xì)節(jié)請參見“全局狀態(tài)”一節(jié)。

--colors

使用彩色輸出。Windows下,用?ANSICON?或?ConEmu。

本選項(xiàng)有三個(gè)可能的值:

  • never: 完全不使用彩色輸出。當(dāng)未使用?--colors?選項(xiàng)時(shí),這是默認(rèn)值。

  • auto: 如果當(dāng)前終端不支持彩色、或者輸出被管道輸出至其他命令、或輸出被重定向至文件時(shí),不使用彩色輸出,其余情況使用彩色。

  • always: 總是使用彩色輸出,即使當(dāng)前終端不支持彩色、輸出被管道輸出至其他命令、或輸出被重定向至文件。

當(dāng)使用了?--colors?選項(xiàng)但未指定任何值時(shí),將選擇?auto?做為其值。

--columns

定義輸出所使用的列數(shù)。如果將其值定義為?max,則使用當(dāng)前終端所支持的最大列數(shù)。

--stderr

選擇輸出到?STDERR?而非?STDOUT.

--stop-on-error

首次錯(cuò)誤出現(xiàn)后停止執(zhí)行。

--stop-on-failure

首次錯(cuò)誤或失敗出現(xiàn)后停止執(zhí)行。

--stop-on-risky

首次碰到有風(fēng)險(xiǎn)的測試時(shí)停止執(zhí)行。

--stop-on-skipped

首次碰到跳過的測試時(shí)停止執(zhí)行。

--stop-on-incomplete

首次碰到不完整的測試時(shí)停止執(zhí)行。

--verbose

輸出更詳盡的信息,例如不完整或者跳過的測試的名稱。

--debug

輸出調(diào)試信息,例如當(dāng)一個(gè)測試開始執(zhí)行時(shí)輸出其名稱。

--loader

指定要使用的?PHPUnit_Runner_TestSuiteLoader?實(shí)現(xiàn)。

標(biāo)準(zhǔn)的測試套件加載器將在當(dāng)前工作目錄和 PHP 的?include_path?配置指令中指定的每個(gè)目錄內(nèi)查找源文件。諸如?Project_Package_Class?這樣的類名對應(yīng)的源文件名為?Project/Package/Class.php

--repeat

將測試重復(fù)運(yùn)行指定次數(shù)。

--tap

使用?Test Anything Protocol (TAP)?報(bào)告測試進(jìn)度。更多細(xì)節(jié)請參見?第 13 章。

--testdox

將測試進(jìn)度以敏捷文檔方式報(bào)告。更多細(xì)節(jié)請參見?第 12 章

--printer

指定要使用的結(jié)果輸出器(printer)。輸出器類必須擴(kuò)展?PHPUnit_Util_Printer?并且實(shí)現(xiàn)?PHPUnit_Framework_TestListener?接口。

--bootstrap

在測試前先運(yùn)行一個(gè) "bootstrap" PHP 文件。

--configuration,?-c

從 XML 文件中讀取配置信息。更多細(xì)節(jié)請參見附錄 C。

如果?phpunit.xml?或?phpunit.xml.dist?(按此順序)存在于當(dāng)前工作目錄并且使用?--configuration,將自動從此文件中讀取配置。

--no-configuration

忽略當(dāng)前工作目錄下的?phpunit.xml?與?phpunit.xml.dist

--include-path

向 PHP 的?include_path?開頭添加指定路徑(可以多個(gè))。

-d

設(shè)置指定的 PHP 配置選項(xiàng)的值。

注意

請注意,從 4.8 開始,選項(xiàng)不能放在參數(shù)之后。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號