4

Phingでブートストラップファイルを使用してPHPUnitテストスイートを実行するには?

私のアプリの構造:

application/
library/
tests/
  application/
  library/
  bootstrap.php
  phpunit.xml
build.xml

phpunit.xml:

<phpunit bootstrap="./bootstrap.php" colors="true">
    <testsuite name="Application Test Suite">
        <directory>./</directory>
    </testsuite>
    <filter>
        <whitelist>
            <directory
              suffix=".php">../library/</directory>
            <directory
              suffix=".php">../application/</directory>
            <exclude>
                <directory
                  suffix=".phtml">../application/</directory>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

それから:

cd /path/to/app/tests/
phpunit
#all test passed

しかし、どうすれば/path/to/app/dir からテストを実行できますか? 問題は、bootstrap.phpライブラリとアプリケーションへの相対パスに依存することです。

実行するphpunit --configuration tests/phpunit.xml /testsと、たくさんのファイルが見つからないというエラーが発生しました。

同じ方法でテストを実行するためのbuild.xmlファイルを作成するにはどうすればよいですか?phingphpunit.xml

4

2 に答える 2

4

ユニットテストを初期化するための小さな PHP スクリプトを作成するのが最善の方法だと思います。次のことを行っています。

私の phpunit.xml / bootstrap="./initalize.php" で

initialize.php

define('BASE_PATH', realpath(dirname(__FILE__) . '/../'));
define('APPLICATION_PATH', BASE_PATH . '/application');

// Include path
set_include_path(
    '.'
    . PATH_SEPARATOR . BASE_PATH . '/library'
    . PATH_SEPARATOR . get_include_path()
);

// Define application environment
define('APPLICATION_ENV', 'testing');
require_once 'BaseTest.php';

BaseTest.php

abstract class BaseTest extends Zend_Test_PHPUnit_ControllerTestCase
{

/**
 * Application
 *
 * @var Zend_Application
 */
public $application;

/**
 * SetUp for Unit tests
 *
 * @return void
 */
public function setUp()
{
    $session = new Zend_Session_Namespace();
    $this->application = new Zend_Application(
                    APPLICATION_ENV,
                    APPLICATION_PATH . '/configs/application.ini'
    );

    $this->bootstrap = array($this, 'appBootstrap');

    Zend_Session::$_unitTestEnabled;

    parent::setUp();
}

/**
 * Bootstrap
 *
 * @return void
 */
public function appBootstrap()
{
    $this->application->bootstrap();
}
}

私のユニットテストはすべてBaseTestクラスを拡張しています。それは魅力のように機能します。

于 2010-09-10T14:32:19.623 に答える
3

Phing で PHPUnit タスクを使用する場合、次のようにブートストラップ ファイルを含めることができます。

<target name="test">
    <phpunit bootstrap="tests/bootstrap.php">
        <formatter type="summary" usefile="false" />
        <batchtest>
            <fileset dir="tests">
                <include name="**/*Test.php"/>
            </fileset>
        </batchtest>
    </phpunit> 
</target> 
于 2010-11-10T13:54:10.097 に答える