附录 C. XML 配置文件

PHPUnit

<phpunit> 元素的属性用于配置 PHPUnit 的核心功能。

  1. <phpunit
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/4.8/phpunit.xsd"
  4. backupGlobals="true"
  5. backupStaticAttributes="false"
  6. <!--bootstrap="/path/to/bootstrap.php"-->
  7. cacheTokens="false"
  8. colors="false"
  9. convertErrorsToExceptions="true"
  10. convertNoticesToExceptions="true"
  11. convertWarningsToExceptions="true"
  12. forceCoversAnnotation="false"
  13. mapTestClassNameToCoveredClassName="false"
  14. printerClass="PHPUnit_TextUI_ResultPrinter"
  15. <!--printerFile="/path/to/ResultPrinter.php"-->
  16. processIsolation="false"
  17. stopOnError="false"
  18. stopOnFailure="false"
  19. stopOnIncomplete="false"
  20. stopOnSkipped="false"
  21. stopOnRisky="false"
  22. testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader"
  23. <!--testSuiteLoaderFile="/path/to/StandardTestSuiteLoader.php"-->
  24. timeoutForSmallTests="1"
  25. timeoutForMediumTests="10"
  26. timeoutForLargeTests="60"
  27. verbose="false">
  28. <!-- ... -->
  29. </phpunit>

以上 XML 配置对应于在“命令行选项”一节描述过的 TextUI 测试执行器的默认行为。

其他那些不能用命令行选项来配置的选项有:

convertErrorsToExceptions

默认情况下,PHPUnit 将会安插一个错误处理函数来将以下错误转换为异常:


- E_WARNING
- E_NOTICE
- E_USER_ERROR
- E_USER_WARNING
- E_USER_NOTICE
- E_STRICT
- E_RECOVERABLE_ERROR
- E_DEPRECATED
- E_USER_DEPRECATED

convertErrorsToExceptions 设为 false 可以禁用此功能。
convertNoticesToExceptions

此选项设置为 false 时,由 convertErrorsToExceptions 安插的错误处理函数不会将 E_NOTICEE_USER_NOTICEE_STRICT 错误转换为异常。
convertWarningsToExceptions

此选项设置为 false 时,由 convertErrorsToExceptions 安插的错误处理函数不会将 E_WARNINGE_USER_WARNING 错误转换为异常。
forceCoversAnnotation

只记录使用了 @covers 标注(文档参见“@covers”一节)的测试的代码覆盖率。
timeoutForLargeTests

如果实行了基于测试规模的时间限制,那么此属性为所有标记为 @large 的测试设定超时限制。在配置的时间限制内未执行完毕的测试将视为失败。
timeoutForMediumTests

如果实行了基于测试规模的时间限制,那么此属性为所有标记为 @medium 的测试设定超时限制。在配置的时间限制内未执行完毕的测试将视为失败。
timeoutForSmallTests

如果实行了基于测试规模的时间限制,那么此属性为所有未标记为 @medium@large 的测试设定超时限制。在配置的时间限制内未执行完毕的测试将视为失败。

测试套件

带有一个或多个 <testsuite> 子元素的 <testsuites> 元素用于将测试套件及测试用例组合出新的测试套件。

  1. <testsuites>
  2. <testsuite name="My Test Suite">
  3. <directory>/path/to/*Test.php files</directory>
  4. <file>/path/to/MyTest.php</file>
  5. <exclude>/path/to/exclude</exclude>
  6. </testsuite>
  7. </testsuites>

可以用 phpVersionphpVersionOperator 属性来指定 PHP 版本需求。在以下例子中,仅当 PHP 版本至少为 5.3.0 时才会将 /path/to/*Test.php 文件与 /path/to/MyTest.php 文件添加到测试套件中。

  1. <testsuites>
  2. <testsuite name="My Test Suite">
  3. <directory suffix="Test.php" phpVersion="5.3.0" phpVersionOperator=">=">/path/to/files</directory>
  4. <file phpVersion="5.3.0" phpVersionOperator=">=">/path/to/MyTest.php</file>
  5. </testsuite>
  6. </testsuites>

phpVersionOperator 属性是可选的,其默认值为 >=

分组

<groups> 元素及其 <include><exclude><group> 子元素用于从带有 @group 标注(相关文档参见 “@group”一节)的测试中选择需要运行(或不运行)的分组。

  1. <groups>
  2. <include>
  3. <group>name</group>
  4. </include>
  5. <exclude>
  6. <group>name</group>
  7. </exclude>
  8. </groups>

以上 XML 配置对应于以如下选项调用 TextUI 测试执行器:

  • —group name

  • —exclude-group name

为代码覆盖率包含或排除文件

<filter> 元素及其子元素用于为代码覆盖率报告配置黑名单与白名单。

  1. <filter>
  2. <blacklist>
  3. <directory suffix=".php">/path/to/files</directory>
  4. <file>/path/to/file</file>
  5. <exclude>
  6. <directory suffix=".php">/path/to/files</directory>
  7. <file>/path/to/file</file>
  8. </exclude>
  9. </blacklist>
  10. <whitelist processUncoveredFilesFromWhitelist="true">
  11. <directory suffix=".php">/path/to/files</directory>
  12. <file>/path/to/file</file>
  13. <exclude>
  14. <directory suffix=".php">/path/to/files</directory>
  15. <file>/path/to/file</file>
  16. </exclude>
  17. </whitelist>
  18. </filter>

Logging (日志记录)

<logging> 元素及其 <log> 子元素用于配置测试执行期间的日志记录。

  1. <logging>
  2. <log type="coverage-html" target="/tmp/report" lowUpperBound="35"
  3. highLowerBound="70"/>
  4. <log type="coverage-clover" target="/tmp/coverage.xml"/>
  5. <log type="coverage-php" target="/tmp/coverage.serialized"/>
  6. <log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
  7. <log type="json" target="/tmp/logfile.json"/>
  8. <log type="tap" target="/tmp/logfile.tap"/>
  9. <log type="junit" target="/tmp/logfile.xml" logIncompleteSkipped="false"/>
  10. <log type="testdox-html" target="/tmp/testdox.html"/>
  11. <log type="testdox-text" target="/tmp/testdox.txt"/>
  12. </logging>

以上 XML 配置对应于以如下选项调用 TextUI 测试执行器:

  • —coverage-html /tmp/report

  • —coverage-clover /tmp/coverage.xml

  • —coverage-php /tmp/coverage.serialized

  • —coverage-text

  • —log-json /tmp/logfile.json

  • > /tmp/logfile.txt

  • —log-tap /tmp/logfile.tap

  • —log-junit /tmp/logfile.xml

  • —testdox-html /tmp/testdox.html

  • —testdox-text /tmp/testdox.txt

lowUpperBoundhighLowerBoundlogIncompleteSkippedshowUncoveredFiles 属性没有等价的 TextUI 测试执行器选项。

  • lowUpperBound:视为“低”覆盖率的最大覆盖率百分比。

  • highLowerBound:视为“高”覆盖率的最小覆盖率百分比。

  • showUncoveredFiles:在 —coverage-text 输出中显示所有符合白名单的文件,不仅限于有覆盖率信息的那些。

  • showOnlySummary:在 —coverage-text 输出中只显示摘要。

测试监听器

<listeners> 元素及其 <listener> 子元素用于在测试执行期间附加额外的测试监听器。

  1. <listeners>
  2. <listener class="MyListener" file="/optional/path/to/MyListener.php">
  3. <arguments>
  4. <array>
  5. <element key="0">
  6. <string>Sebastian</string>
  7. </element>
  8. </array>
  9. <integer>22</integer>
  10. <string>April</string>
  11. <double>19.78</double>
  12. <null/>
  13. <object class="stdClass"/>
  14. </arguments>
  15. </listener>
  16. </listeners>

以上 XML 配置对应于将 $listener 对象(见下文)附到测试执行过程上。

  1. $listener = new MyListener(
  2. array('Sebastian'),
  3. 22,
  4. 'April',
  5. 19.78,
  6. NULL,
  7. new stdClass
  8. );

设定 PHP INI 设置、常量、全局变量

<php> 元素及其子元素用于配置 PHP 设置、常量以及全局变量。同时也可用于向 include_path 前部置入内容。

  1. <php>
  2. <includePath>.</includePath>
  3. <ini name="foo" value="bar"/>
  4. <const name="foo" value="bar"/>
  5. <var name="foo" value="bar"/>
  6. <env name="foo" value="bar"/>
  7. <post name="foo" value="bar"/>
  8. <get name="foo" value="bar"/>
  9. <cookie name="foo" value="bar"/>
  10. <server name="foo" value="bar"/>
  11. <files name="foo" value="bar"/>
  12. <request name="foo" value="bar"/>
  13. </php>

以上 XML 配置对应于如下 PHP 代码:

  1. ini_set('foo', 'bar');
  2. define('foo', 'bar');
  3. $GLOBALS['foo'] = 'bar';
  4. $_ENV['foo'] = 'bar';
  5. $_POST['foo'] = 'bar';
  6. $_GET['foo'] = 'bar';
  7. $_COOKIE['foo'] = 'bar';
  8. $_SERVER['foo'] = 'bar';
  9. $_FILES['foo'] = 'bar';
  10. $_REQUEST['foo'] = 'bar';

为 Selenium RC 配置浏览器

<selenium> 元素及其 <browser> 子元素用于配置 Selenium RC 服务器列表。

  1. <selenium>
  2. <browser name="Firefox on Linux"
  3. browser="*firefox /usr/lib/firefox/firefox-bin"
  4. host="my.linux.box"
  5. port="4444"
  6. timeout="30000"/>
  7. </selenium>

以上 XML 配置对应于如下 PHP 代码:

  1. class WebTest extends PHPUnit_Extensions_SeleniumTestCase
  2. {
  3. public static $browsers = array(
  4. array(
  5. 'name' => 'Firefox on Linux',
  6. 'browser' => '*firefox /usr/lib/firefox/firefox-bin',
  7. 'host' => 'my.linux.box',
  8. 'port' => 4444,
  9. 'timeout' => 30000
  10. )
  11. );
  12.  
  13. // ...
  14. }

原文: http://www.phpunit.cn/manual/4.8/zh_cn/appendixes.configuration.html