2010-09-09 61 views
4

如何使用phing运行带bootstrap文件的PHPUnit测试套件?phing和phpunit + bootstrap

我的应用程序结构:

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文件phing以与phpunit.xml相同的方式运行测试?

回答

4

我认为最好的方法是创建一个小的PHP脚本initalize你的单元测试,IAM执行以下操作:

在我phpunit.xml /引导= “./ 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 Class,它的功能就像一个魅力。

+1

感谢。这或多或少是我的'bootstrap.php'。我的问题是我不知道我可以用这种方式指定引导参数:'phpunit --bootstrap tests/bootstrap.php --configuration tests/phpunit.xml' – takeshin 2010-09-10 18:10:03

+0

这对我来说很新鲜,感谢评论! – opHASnoNAME 2010-09-11 06:10:09

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> 
+0

不幸的是,在phing中以这种方式调用phpunit时,您不能使用phpunit.xml。 – 2012-09-05 14:16:19