Prev Next

付録C XML 設定ファイル

PHPUnit

<phpunit> 要素の属性を使って PHPUnit のコア機能を設定します。

<phpunit backupGlobals="false"
         backupStaticAttributes="true"
         bootstrap="/path/to/bootstrap.php"
         colors="false"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="true"
         stopOnFailure="true"
         syntaxCheck="false"
         testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader">
  <!-- ... -->
</phpunit>

上の XML 設定ファイルは、 TextUI テストランナーを以下の引数で起動します。

  • --no-globals-backup

  • --static-backup

  • --bootstrap /path/to/bootstrap.php

  • --colors

  • --process-isolation

  • --stop-on-failure

  • --no-syntax-check

  • --loader PHPUnit_Runner_StandardTestSuiteLoader

convertErrorsToExceptionsconvertNoticesToExceptions および convertWarningsToExceptions 属性には、TextUI テストランナーで対応するスイッチがありません。

テストスイート

<testsuites> 要素とその子要素である <testsuite> を使って、 テストスイート群やテストケース群の中からテストスイートを構成します。

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

グループ

<groups> 要素とその子要素である <include><exclude> および <group> を使って、 テストスイートの中から実行する (しない) テストグループを選びます。

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

上の XML 設定ファイルは、 TextUI テストランナーを以下の引数で起動します。

  • --group name

  • --exclude-group name

コードカバレッジ対象のファイルの追加や除外

<filter> 要素とその子要素を使って、 コードカバレッジレポートのブラックリストとホワイトリストを設定します。

<filter>
  <blacklist>
    <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>
  </blacklist>
  <whitelist>
    <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>

上の XML 設定ファイルは、 次のような PHPUnit_Util_Filter クラスを使用することに対応します。

PHPUnit_Util_Filter::addDirectoryToFilter(
  '/path/to/files', '.php'
);

PHPUnit_Util_Filter::addFileToFilter('/path/to/file');

PHPUnit_Util_Filter::removeDirectoryFromFilter(
  '/path/to/files', '.php'
);

PHPUnit_Util_Filter::removeFileFromFilter('/path/to/file');

PHPUnit_Util_Filter::addDirectoryToWhitelist(
  '/path/to/files', '.php'
);

PHPUnit_Util_Filter::addFileToWhitelist('/path/to/file');

PHPUnit_Util_Filter::removeDirectoryFromWhitelist(
  '/path/to/files', '.php'
);

PHPUnit_Util_Filter::removeFileFromWhitelist('/path/to/file');

ログ出力

<logging> 要素とその子要素である <log> を使って、 テストの実行結果のログ出力を設定します。

<logging>
  <log type="coverage-html" target="/tmp/report" charset="UTF-8"
       yui="true" highlight="false"
       lowUpperBound="35" highLowerBound="70"/>
  <log type="coverage-xml" target="/tmp/coverage.xml"/>
  <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-xml /tmp/coverage.xml

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

charsetyuihighlightlowUpperBoundhighLowerBound および logIncompleteSkipped 属性には、TextUI テストランナーで対応するスイッチがありません。

テストリスナー

<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(
  array('Sebastian'),
  22,
  'April',
  19.78,
  NULL,
  new stdClass
);

PHP INI 項目や定数、グローバル変数の設定

<php> 要素とその子要素を使って、 PHP の設定や定数、グローバル変数を設定します。

<php>
  <ini name="foo" value="bar"/>
  <const name="foo" value="bar"/>
  <var name="foo" value="bar"/>
</php>

上の XML 設定は、次の PHP コードに対応します。

ini_set('foo', 'bar');
define('foo', 'bar');
$GLOBALS['foo'] = 'bar';

Selenium RC の設定ブラウザ

<selenium> 要素とその子要素である <browser> を使って、 Selenium RC サーバのリストを設定します。

<selenium>
  <browser name="Firefox on Linux"
           browser="*firefox /usr/lib/firefox/firefox-bin"
           host="my.linux.box"
           port="4444"
           timeout="30000"/>
</selenium>

上の XML 設定は、次の PHP コードに対応します。

class WebTest extends PHPUnit_Extensions_SeleniumTestCase
{
    public static $browsers = array(
      array(
        'name'    => 'Firefox on Linux',
        'browser' => '*firefox /usr/lib/firefox/firefox-bin',
        'host'    => 'my.linux.box',
        'port'    => 4444,
        'timeout' => 30000
      )
    );

    // ...
}
Prev Next
1. 自動テスト
2. PHPUnit の目標
3. PHPUnit のインストール
4. PHPUnit 用のテストの書き方
テストの依存性
データプロバイダ
例外のテスト
PHP のエラーのテスト
5. コマンドラインのテストランナー
6. Fixtures
tearDown() よりも setUp()
バリエーション
Fixture の共有
グローバルな状態
7. テストの構成
ファイルシステムを用いたテストスイートの構成
XML 設定ファイルを用いたテストスイートの構成
テストケースクラスの使用法
8. テストケースの拡張
出力内容のテスト
9. データベースのテスト
データセット
Flat XML データセット
XML データセット
CSV Data Set
データセットの交換
操作
データベースのテストのコツ
10. 不完全なテスト・テストの省略
不完全なテスト
テストの省略
11. テストダブル
スタブ
モックオブジェクト
ウェブサービスのスタブおよびモック
ファイルシステムのモック
12. テストの進め方
開発中のテスト
デバッグ中のテスト
13. テスト駆動開発
銀行口座の例
14. 振舞駆動開発
ボウリングゲームの例
15. コードカバレッジ解析
カバーするメソッドの指定
コードブロックの無視
ファイルのインクルードや除外
16. テストのその他の使用法
アジャイルな文書作成
複数チームでのテスト
17. 雛形ジェネレータ
テストケースクラスの雛形の作成
テストケースクラスからのクラスの雛形の作成
18. PHPUnit と Selenium
Selenium RC
PHPUnit_Extensions_SeleniumTestCase
19. ログ出力
テスト結果 (XML)
テスト結果 (TAP)
テスト結果 (JSON)
コードカバレッジ (XML)
20. ビルドの自動化
Apache Ant
Apache Maven
Phing
21. 継続的インテグレーション
Atlassian Bamboo
CruiseControl
phpUnderControl
22. PHPUnit API
概要
PHPUnit_Framework_Assert
assertArrayHasKey()
assertClassHasAttribute()
assertClassHasStaticAttribute()
assertContains()
assertContainsOnly()
assertEqualXMLStructure()
assertEquals()
assertFalse()
assertFileEquals()
assertFileExists()
assertGreaterThan()
assertGreaterThanOrEqual()
assertLessThan()
assertLessThanOrEqual()
assertNull()
assertObjectHasAttribute()
assertRegExp()
assertSame()
assertSelectCount()
assertSelectEquals()
assertSelectRegExp()
assertStringEndsWith()
assertStringEqualsFile()
assertStringStartsWith()
assertTag()
assertThat()
assertTrue()
assertType()
assertXmlFileEqualsXmlFile()
assertXmlStringEqualsXmlFile()
assertXmlStringEqualsXmlString()
PHPUnit_Framework_Test
PHPUnit_Framework_TestCase
PHPUnit_Framework_TestSuite
PHPUnit_Framework_TestResult
パッケージの構成
23. PHPUnit の拡張
PHPUnit_Framework_TestCase のサブクラスの作成
アサートクラスの作成
PHPUnit_Extensions_TestDecorator のサブクラスの作成
PHPUnit_Framework_Test の実装
PHPUnit_Framework_TestResult のサブクラスの作成
PHPUnit_Framework_TestListener の実装
新しいテストランナーの作成
A. アサーション
B. アノテーション
@assert
@backupGlobals
@backupStaticAttributes
@covers
@dataProvider
@depends
@expectedException
@group
@outputBuffering
@runTestsInSeparateProcesses
@runInSeparateProcess
@test
@testdox
@ticket
C. XML 設定ファイル
PHPUnit
テストスイート
グループ
コードカバレッジ対象のファイルの追加や除外
ログ出力
テストリスナー
PHP INI 項目や定数、グローバル変数の設定
Selenium RC の設定ブラウザ
D. 目次
E. 参考文献
F. 著作権