phing 和 phpunit +引导程序

发布于 2024-09-18 14:07:08 字数 1211 浏览 1 评论 0原文

如何使用 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/ 目录运行测试?问题是,bootstrap.php 依赖于库和应用程序的相对路径。

如果我运行 phpunit --configuration tests/phpunit.xml /tests ,我会收到一堆文件未找到错误。

如何为 phing 编写 build.xml 文件来以与 phpunit.xml 相同的方式运行测试?

How to run PHPUnit test suite with bootstrap file with phing?

My app structure:

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>

then:

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

But how do I run the tests from /path/to/app/ dir? The problem is, that the bootstrap.php depends on relative paths to library and application.

If I run phpunit --configuration tests/phpunit.xml /tests I got a bunch of file not found errors.

How do I write build.xml file for phing to run the tests the same way phpunit.xml does?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

递刀给你 2024-09-25 14:07:08

我认为最好的方法是创建一个小的 PHP 脚本来初始化单元测试,我正在执行以下操作:

在我的 phpunit.xml / bootstrap="./initalize.php"

initalize.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 类,它的工作方式就像一个魅力。

I Think the best way is to create an small PHP Script to initalize your Unit tests, iam doing the following:

In my phpunit.xml / bootstrap="./initalize.php"

initalize.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();
}
}

All my Unit tests are extending BaseTest Class, it works like a charm.

上课铃就是安魂曲 2024-09-25 14:07:08

当您在 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> 

When you use the PHPUnit tasks in Phing, you can include your bootstrap file like this:

<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> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文