首页
学习
活动
专区
工具
TVP
发布

The XML Configuration File

PHPUnit

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

<phpunit
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
         backupGlobals="true"
         backupStaticAttributes="false"
         <!--bootstrap="/path/to/bootstrap.php"-->
         cacheTokens="false"
         colors="false"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         forceCoversAnnotation="false"
         mapTestClassNameToCoveredClassName="false"
         printerClass="PHPUnit_TextUI_ResultPrinter"
         <!--printerFile="/path/to/ResultPrinter.php"-->
         processIsolation="false"
         stopOnError="false"
         stopOnFailure="false"
         stopOnIncomplete="false"
         stopOnSkipped="false"
         stopOnRisky="false"
         testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader"
         <!--testSuiteLoaderFile="/path/to/StandardTestSuiteLoader.php"-->
         timeoutForSmallTests="1"
         timeoutForMediumTests="10"
         timeoutForLargeTests="60"
         verbose="false">
  <!-- ... -->
</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

设置convertErrorsToExceptionsfalse禁用此功能。

convertNoticesToExceptions

当设置为false,通过安装在错误处理程序convertErrorsToExceptions不会转换E_NOTICEE_USER_NOTICE或者E_STRICT错误例外。

convertWarningsToExceptions

设置false为时,安装的错误处理程序convertErrorsToExceptions不会将其转换E_WARNINGE_USER_WARNING发生异常。

forceCoversAnnotation

只有使用@covers“@covers”一节中记录的注释的测试才会记录代码覆盖率。

timeoutForLargeTests

如果基于测试大小的时间限制被强制执行,那么这个属性为所有标记为的测试设置超时@large。如果测试未在其配置的超时内完成,则会失败。

timeoutForMediumTests

如果基于测试大小的时间限制被强制执行,那么这个属性为所有标记为的测试设置超时@medium。如果测试未在其配置的超时内完成,则会失败。

timeoutForSmallTests

如果基于测试大小的时间限制被强制执行,那么这个属性为没有标记为@medium或的所有测试设置超时@large。如果测试未在其配置的超时内完成,则会失败。

测试套件

<testsuites>元素及其一个或多个<testsuite>孩子可以用来构成一个测试套件出测试套件和测试用例。

<testsuites>
  <testsuite name="My Test Suite">
    <directory>/path/to/*Test.php files</directory>
    <file>/path/to/MyTest.php</file>
    <exclude>/path/to/exclude</exclude>
  </testsuite>
</testsuites>

使用phpVersionphpVersionOperator属性,可以指定一个必需的PHP版本。下面的例子只会添加/path/to/*Test.php文件和/path/to/MyTest.php文件,如果PHP版本至少5.3.0。

  <testsuites>
    <testsuite name="My Test Suite">
      <directory suffix="Test.php" phpVersion="5.3.0" phpVersionOperator=">=">/path/to/files</directory>
      <file phpVersion="5.3.0" phpVersionOperator=">=">/path/to/MyTest.php</file>
    </testsuite>
  </testsuites>

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

<groups>元素及其<include><exclude><group>孩子们可以用来选择标有组测试@group应该(不)来运行,注释(在被称为“@group”一节记载)。

<groups>
  <include>
    <group>name</group>
  </include>
  <exclude>
    <group>name</group>
  </exclude>
</groups>

上面的XML配置对应于使用以下选项调用TextUI测试运行器:

  • --group name
  • --exclude-group name

将代码覆盖率的白名单文件

<filter>元素及其子可以用于配置代码覆盖率报告的白名单。

<filter>
  <whitelist processUncoveredFilesFromWhitelist="true">
    <directory suffix=".php">/path/to/files</directory>
    <file>/path/to/file</file>
    <exclude>
      <directory suffix=".php">/path/to/files</directory>
      <file>/path/to/file</file>
    </exclude>
  </whitelist>
</filter>

记录

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

<logging>
  <log type="coverage-html" target="/tmp/report" lowUpperBound="35"
       highLowerBound="70"/>
  <log type="coverage-clover" target="/tmp/coverage.xml"/>
  <log type="coverage-php" target="/tmp/coverage.serialized"/>
  <log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
  <log type="junit" target="/tmp/logfile.xml" logIncompleteSkipped="false"/>
  <log type="testdox-html" target="/tmp/testdox.html"/>
  <log type="testdox-text" target="/tmp/testdox.txt"/>
</logging>

上面的XML配置对应于使用以下选项调用TextUI测试运行器:

  • --coverage-html /tmp/report
  • --coverage-clover /tmp/coverage.xml
  • --coverage-php /tmp/coverage.serialized
  • --coverage-text
  • /tmp/logfile.txt
  • --log-junit /tmp/logfile.xml
  • --testdox-html /tmp/testdox.html
  • --testdox-text /tmp/testdox.txt

lowUpperBoundhighLowerBoundlogIncompleteSkippedshowUncoveredFiles属性没有等价文本界面的测试选项。

  • lowUpperBound:最大覆盖百分比被认为“低”覆盖。
  • highLowerBound:最低覆盖率被认为“高度”覆盖。
  • showUncoveredFiles:在--coverage-text输出中显示所有列入白名单的文件,而不仅仅是具有覆盖信息的文件。
  • showOnlySummary:仅显示--coverage-text输出中的摘要。

测试

<listeners>元素及其<listener>孩子可以用于连接其它测试听众测试执行。

<listeners>
  <listener class="MyListener" file="/optional/path/to/MyListener.php">
    <arguments>
      <array>
        <element key="0">
          <string>Sebastian</string>
        </element>
      </array>
      <integer>22</integer>
      <string>April</string>
      <double>19.78</double>
      <null/>
      <object class="stdClass"/>
    </arguments>
  </listener>
</listeners>

上面的XML配置对应于将$listener对象(见下文)附加到测试执行:

$listener = new MyListener(
    ['Sebastian'],
    22,
    'April',
    19.78,
    null,
    new stdClass
);

设置PHP INI设置,常量和全局变量

The `<php>` element and its children can be used to configure PHP settings, constants, and global variables. It can also be used to prepend the `include_path`. 
<php>
  <includePath>.</includePath>
  <ini name="foo" value="bar"/>
  <const name="foo" value="bar"/>
  <var name="foo" value="bar"/>
  <env name="foo" value="bar"/>
  <post name="foo" value="bar"/>
  <get name="foo" value="bar"/>
  <cookie name="foo" value="bar"/>
  <server name="foo" value="bar"/>
  <files name="foo" value="bar"/>
  <request name="foo" value="bar"/>
</php>

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

ini_set('foo', 'bar');
define('foo', 'bar');
$GLOBALS['foo'] = 'bar';
$_ENV['foo'] = 'bar';
$_POST['foo'] = 'bar';
$_GET['foo'] = 'bar';
$_COOKIE['foo'] = 'bar';
$_SERVER['foo'] = 'bar';
$_FILES['foo'] = 'bar';
$_REQUEST['foo'] = 'bar';

扫码关注腾讯云开发者

领取腾讯云代金券