我有一个Zend Framework项目,并希望使用单元测试来测试它.
在tests文件夹中,我有phpunit.xml以下内容:
<phpunit bootstrap="./application/bootstrap.php" colors="true">
<testsuite name="Application Test Suite">
<directory>./</directory>
</testsuite>
<filter>
<whitelist>
<directory suffix=".php">../application/</directory>
<exclude>
<directory suffix=".phtml">../application/</directory>
<file>../application/Bootstrap.php</file>
<file>../application/controllers/ErrorController.php</file>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./log/reprot" charset="UTP-8"
yui="true" highlight = "true" lowUpoerBound="50" highLowerBound="80"/>
<log type="textdox" target="./log/testdox.html" />
</logging>
Run Code Online (Sandbox Code Playgroud)
我bootstrap.php在/ tests/application文件夹中有如下内容:
<?php
error_reporting(E_ALL | E_STRICT);
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
require_once 'Zend/Loader/Autoloader.php';
require_once 'controllers/ControllerTestCase.php';
Zend_Loader_Autoloader::getInstance();
Run Code Online (Sandbox Code Playgroud)
当我转到命令行时,运行以下命令
phpunit --configuration phpunit.xml
Run Code Online (Sandbox Code Playgroud)
它抛出异常:
PHP Fatal error: Uncaught exception 'PHPUnit_Framework_Exception' with message
'Neither "Application Test Suite.php" nor "Application Test Suite.php" could be
opened.' in D:\PHP\php5\PEAR\PHPUnit\Util\Skeleton\Test.php:102
Stack trace:
#0 D:\PHP\php5\PEAR\PHPUnit\TextUI\Command.php(157): PHPUnit_Util_Skeleton_Test-
>__construct('Application Tes...', '')
#1 D:\PHP\php5\PEAR\PHPUnit\TextUI\Command.php(129): PHPUnit_TextUI_Command->run
(Array, true)
#2 D:\PHP\php5\phpunit(53): PHPUnit_TextUI_Command::main()
#3 {main}
thrown in D:\PHP\php5\PEAR\PHPUnit\Util\Skeleton\Test.php on line 102
我该怎么办呢?
我在同样的设置下遇到了同样的问题.据我所知,更新版本的PHPUnit需要至少一个实际测试才能正常运行.创建一个简单的测试后,它工作.
myProject的/测试/应用/ DemoTest.php
<?php
class DemoTest extends ControllerTestCase
{
public function setUp() {
parent::setUp();
}
public function testCanDoUnitTest() {
$this->assertTrue(true);
}
}
Run Code Online (Sandbox Code Playgroud)
你会注意到它抛出异常,因为它寻找的文件名与name你为测试套件提供的文件相同.实际上你需要编写一个测试套件,然后提供该测试套件的名称到您的配置:http://www.phpunit.de/manual/3.2/en/organizing-test-suites.html