我想爲我的Zend Framework/Doctrine 2.0應用程序編寫單元測試,但我不太瞭解如何在ZF中設置單元測試。另外,我想在這些單元測試中包含Doctrine 2.0。我將如何着手設置?你能指點我一個例子嗎?使用Zend Framework/Doctrine 2.0進行單元測試
謝謝
我想爲我的Zend Framework/Doctrine 2.0應用程序編寫單元測試,但我不太瞭解如何在ZF中設置單元測試。另外,我想在這些單元測試中包含Doctrine 2.0。我將如何着手設置?你能指點我一個例子嗎?使用Zend Framework/Doctrine 2.0進行單元測試
謝謝
要設置的單元測試,我創建了PHPUnit的(phpunit.xml)的配置文件和TestHelper.php test目錄。配置基本上對phpunit說需要執行哪個單元測試,並且在覆蓋範圍內需要跳過哪些文件夾和文件。在我的配置中,它只是應用程序和庫文件夾中將要執行的所有單元測試文件。
Testhelper需要通過所有單元測試進行擴展。
phpunit.xml
<phpunit bootstrap="./TestHelper.php" colors="true">
<testsuite name="Your Application">
<directory>./application</directory>
<directory>./library</directory>
</testsuite>
<filter>
<whitelist>
<directory suffix=".php">../application/</directory>
<directory suffix=".php">../library/App/</directory>
<exclude>
<directory suffix=".phtml">../application/</directory>
<directory suffix=".php">../application/database</directory>
<directory suffix=".php">../application/models/Entities</directory>
<directory suffix=".php">../application/models/mapping</directory>
<directory suffix=".php">../application/models/proxy</directory>
<directory suffix=".php">../application/views</directory>
<file>../application/Bootstrap.php</file>
<file>../application/modules/admin/controllers/ErrorController.php</file>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./log/report" title="PrintConcept" charset="UTF-8" yui="true" highlight="true" lowUpperBound="35" highLowerBound="70" />
<log type="testdox" target="./log/testdox.html" />
</logging>
</phpunit>
TestHelper.php
<?php
error_reporting(E_ALL | E_STRICT);
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define testing application environment
define('APPLICATION_ENV', 'testing');
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/**
* Zend_Application
*/
require_once 'Zend/Application.php';
/**
* Base Controller Test Class
*
* All controller test should extend this
*/
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
abstract class BaseControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
public function setUp()
{
$application = new Zend_Application(APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini');
$this->bootstrap = array($application->getBootstrap(), 'bootstrap');
Zend_Session::$_unitTestEnabled = true;
parent::setUp();
}
public function tearDown()
{
/* Tear Down Routine */
}
}
這僅涵蓋了ZF和PHPUnit的初始設置
感謝您的反饋。你通常在所有的測試用例中擴展Zend_Test_PHPUnit_ControllerTestCase嗎?或者這個類只能擴展用於控制器測試? – clarkstachio 2010-08-27 03:27:05
我的測試全部擴展了BaseControllerTestCase。但通常情況下,您應該只從BaseControllerTestCase擴展到您的控制器測試以及PHPUnit_Framework_TestCase的所有其他測試 – tom 2010-08-27 06:01:17
我也有興趣在如何將在單元測試中的學說2。我甚至在郵件列表上發佈了一個關於它的問題,但我沒有得到任何答案。 – tom 2010-08-26 07:18:14
我已經在這個問題上取得了一些進展,並且會在幾分鐘後發佈我的設置。你認爲應該在他們的Doctrine 2.0模型層中測試持久性,還是不用擔心這個問題,並將模型作爲普通的php對象來測試? – clarkstachio 2010-08-27 03:25:49
我認爲你應該只測試模型而不是學說。他們編寫自己的單元測試。 你有沒有取得進展? – tom 2010-08-31 08:05:23