PHPUnit-附錄 C. XML 配置文件

【http://www.phpunit.cn/manual/5.7/zh_cn/appendixes.configuration.html】php

 

PHPUnit

<phpunit> 元素的屬性用於配置 PHPUnit 的核心功能。html

<phpunit
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.5/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 測試執行器的默認行爲。json

其餘那些不能用命令行選項來配置的選項有:bootstrap

convertErrorsToExceptions

默認狀況下,PHPUnit 將會安插一個錯誤處理函數來將如下錯誤轉換爲異常:cookie

  • 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 能夠禁用此功能。app

convertNoticesToExceptions

此選項設置爲 false 時,由 convertErrorsToExceptions 安插的錯誤處理函數不會將 E_NOTICEE_USER_NOTICEE_STRICT 錯誤轉換爲異常。函數

convertWarningsToExceptions

此選項設置爲 false 時,由 convertErrorsToExceptions 安插的錯誤處理函數不會將 E_WARNINGE_USER_WARNING 錯誤轉換爲異常。post

forceCoversAnnotation

只記錄使用了 @covers 標註(文檔參見「@covers」一節)的測試的代碼覆蓋率。測試

timeoutForLargeTests

若是實行了基於測試規模的時間限制,那麼此屬性爲全部標記爲 @large 的測試設定超時限制。在配置的時間限制內未執行完畢的測試將視爲失敗。ui

timeoutForMediumTests

若是實行了基於測試規模的時間限制,那麼此屬性爲全部標記爲 @medium 的測試設定超時限制。在配置的時間限制內未執行完畢的測試將視爲失敗。

timeoutForSmallTests

若是實行了基於測試規模的時間限制,那麼此屬性爲全部未標記爲 @medium@large 的測試設定超時限制。在配置的時間限制內未執行完畢的測試將視爲失敗。

測試套件

帶有一個或多個 <testsuite> 子元素的 <testsuites> 元素用於將測試套件及測試用例組合出新的測試套件。

<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 版本需求。在如下例子中,僅當 PHP 版本至少爲 5.3.0 時纔會將 /path/to/*Test.php 文件與 /path/to/MyTest.php 文件添加到測試套件中。

  <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

Whitelisting Files for Code Coverage

<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 (日誌記錄)

<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="json" target="/tmp/logfile.json"/>
  <log type="tap" target="/tmp/logfile.tap"/>
  <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

  • --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> 子元素用於在測試執行期間附加額外的測試監聽器。

<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 設置、常量、全局變量

<php> 元素及其子元素用於配置 PHP 設置、常量以及全局變量。同時也可用於向 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';
相關文章
相關標籤/搜索