2010-09-09 57 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