The Command-Line Test Runner

命令行测试运行器

PHPUnit命令行测试运行器可以通过该phpunit命令调用。以下代码显示如何使用PHPUnit命令行测试运行器运行测试:

phpunit ArrayTest PHPUnit 6.4.0 by Sebastian Bergmann and contributors. .. Time: 0 seconds OK (2 tests, 2 assertions)

如上所示调用时,PHPUnit命令行测试运行器将ArrayTest.php在当前工作目录中查找源文件,加载它,并期望找到ArrayTest测试用例类。然后它将执行该类的测试。

对于每次测试运行,PHPUnit命令行工具都会打印一个字符以指示进度:

测试成功时打印。

F

在运行测试方法时断言失败时打印。

E

运行测试方法时发生错误时打印。

R

当测试被标记为有风险时打印(见第6章)。

S

测试被跳过时打印(见第7章)。

I

当测试被标记为不完整或尚未实施时打印(见第7章)。

PHPUnit区分故障错误。失败是违反的PHPUnit断言,如失败的assertEquals()调用。错误是意外的异常或PHP错误。有时候这种区分证明是有用的,因为错误往往比失败更容易解决。如果你有一大堆问题,最好先解决这些错误,看看它们全部修复后是否还有任何故障

命令行选项

我们来看看下面代码中的命令行测试运行器的选项:

phpunit --help PHPUnit 6.4.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. --whitelist <dir> Whitelist <dir> for code coverage analysis. --disable-coverage-ignore Disable annotations for ignoring code coverage. 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 Test Selection Options: --filter <pattern> Filter which tests to run. --testsuite <name,...> 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. --list-suites List available test suites. --test-suffix ... 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. --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-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-warning Treat tests with warnings as failures. --fail-on-risky Treat risky tests as failures. -v|--verbose Output more verbose information. --debug Display debugging information. --loader <loader> TestSuiteLoader implementation to use. --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). --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). --no-coverage Ignore code coverage configuration. --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. --generate-configuration Generate configuration file with suggested settings. 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.

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章。

请注意,此功能仅在安装了标记器和Xdebug扩展时才可用。

--coverage-crap4j

以Crap4j格式生成代码覆盖率报告。更多细节见第11章。

请注意,此功能仅在安装了标记器和Xdebug扩展时才可用。

--coverage-html

以HTML格式生成代码覆盖率报告。更多细节见第11章。

请注意,此功能仅在安装了标记器和Xdebug扩展时才可用。

--coverage-php

使用代码覆盖率信息生成序列化的PHP_CodeCoverage对象。

请注意,此功能仅在安装了标记器和Xdebug扩展时才可用。

--coverage-text

以可读的格式生成日志文件或命令行输出,其中包含运行测试的代码覆盖率信息。更多细节见第13章。

请注意,此功能仅在安装了标记器和Xdebug扩展时才可用。

--log-junit

为测试运行生成JUnit XML格式的日志文件。更多细节见第13章。

--testdox-html and --testdox-text

为运行的测试生成HTML或纯文本格式的敏捷文档。更多细节见第12章。

--filter

只运行名称与给定正则表达式模式相匹配的测试。如果该模式未包含在分隔符中,PHPUnit会将该模式包含在/分隔符中。

要匹配的测试名称将采用以下格式之一:

TestNamespace \ TestCaseClass :: testMethod

默认的测试名称格式相当于__METHOD__在测试方法中使用魔术常量。

TestNamespace \ TestCaseClass :: testMethod与数据集#0

当一个测试有一个数据提供者时,数据的每次迭代都会将当前索引附加到默认测试名称的末尾。

TestNamespace \ TestCaseClass :: testMethod与数据集“我的命名数据”

当测试有一个使用命名集的数据提供者时,数据的每次迭代都会将当前名称附加到默认测试名称的末尾。有关命名数据集的示例,请参见示例3.1。

例3.1:命名数据集

<?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

PHPT测试的测试名称是文件系统路径。

有关过滤器模式的示例,请参见示例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。

例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

只搜索具有指定后缀(es)的测试文件。

--report-useless-tests

严格测试不测试任何东西的测试。详情请参阅第6章。

--strict-coverage

严格限制无意覆盖的代码。详情请参阅第6章。

--strict-global-state

对全局操纵要严格。详情请参阅第6章。

--disallow-test-output

在测试期间严格输出。详情请参阅第6章。

--disallow-todo-tests

不执行@todo在其docblock中具有注释的测试。

--enforce-time-limit

根据测试大小实施时间限制。详情请参阅第6章。

--process-isolation

在单独的PHP进程中运行每个测试。

--no-globals-backup

不要备份和恢复$ GLOBALS。有关更多详细信息,请参阅“全局状态”一节。

--static-backup

备份和恢复用户定义类的静态属性。有关更多详细信息,请参阅“全局状态”一节。

--colors

在输出中使用颜色。在Windows上,使用ANSICONConEmu

这个选项有三种可能的值:

  • never:从不在输出中显示颜色。这是--colors不使用选项时的默认值。

  • auto:在输出中显示颜色,除非当前终端不支持颜色,或者输出被传送到命令或重定向到文件。

  • always:即使当前终端不支持颜色,或者当输出连接到命令或重定向到文件时,始终在输出中显示颜色。

何时--colors使用没有任何价值,auto是选择的价值。

--columns

定义用于进度输出的列数。如果max定义为值,那么列数将是当前终端的最大值。

--stderr

可选择打印STDERR而不是STDOUT

--stop-on-error

第一个错误时停止执行。

--stop-on-failure

Stop execution upon first error or 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

重复运行测试(s)指定的次数。

--testdox

将测试进度作为敏捷文档报告。更多细节见第12章。

--printer

指定要使用的结果打印机。打印机类必须扩展PHPUnit_Util_Printer并实现PHPUnit_Framework_TestListener接口。

--bootstrap

在测试之前运行的“bootstrap”PHP文件。

--configuration, -c

从XML文件读取配置。更多细节见附录C.

如果phpunit.xmlphpunit.xml.dist(按该顺序)存在于当前工作目录--configuration且未被使用,配置将自动从该文件中读取。

--no-configuration

忽略phpunit.xmlphpunit.xml.dist从当前工作目录中。

--include-path

include_path用给定路径加上PHP的前缀。

-d

设置给定的PHP配置选项的值。

请注意,从4.8开始,选项可以放在参数后面。