在Zend Framework 2中为自己的模块执行PHPUnit测试时,我似乎有一个小问题。
操作系统:Mac OS 10.8.3
Zend Framework 2.1.4
PHPUnit Version 3.7.19(通过pear安装)
PHP 5.3.15版本(启用xdebug,版本2.1.3)
我按照Zend指南的说明,创建模块"Album"(http://framework.zend.com/manual/2.1/en/user-guide/modules.html)
我不想把单元测试放在模块文件夹中,我想把它们都放在一个集中的文件夹中。这是我的应用程序的基本结构:
配置
拼
模块
公共
测试
——模块
——专辑
——AlbumTest.php
——Bootstrap.php
——phpunit.xml
供应商
…(许可,自述,作曲家和init_autolloader .php)
->模块相册的测试位于"tests"中的"Modules/Album"文件夹中。文件夹"tests"还包含Bootstrap.php和phpunit.xml。
文件内容如下:
Bootstrap.php
<?php
/*
* Set error reporting to the level to which Zend Framework code must comply.
*/
error_reporting( E_ALL | E_STRICT );
/*
* The first run required me to fix some constants
*/
defined('TESTS_ZEND_FORM_RECAPTCHA_PUBLIC_KEY') || define('TESTS_ZEND_FORM_RECAPTCHA_PUBLIC_KEY', 'public key');
defined('TESTS_ZEND_FORM_RECAPTCHA_PRIVATE_KEY') || define('TESTS_ZEND_FORM_RECAPTCHA_PRIVATE_KEY', 'private key');
defined('TESTS_ZEND_LDAP_PRINCIPAL_NAME') || define('TESTS_ZEND_LDAP_PRINCIPAL_NAME', 'someUser@example.com');
defined('TESTS_ZEND_LDAP_ALT_USERNAME') || define('TESTS_ZEND_LDAP_ALT_USERNAME', 'anotherUser');
chdir(dirname(__DIR__));
/*
* autoload the application
*/
include __DIR__ . '/../init_autoloader.php';
ZendMvcApplication::init(include 'config/test.config.php');
phpunit.xml
<phpunit bootstrap="./Bootstrap.php" colors="true">
<testsuites>
<testsuite name="Zend Module Tests">
<directory>./Modules</directory>
</testsuite>
</testsuites>
</phpunit>
AlbumTest.php
<?php
namespace AlbumTestModel;
use AlbumModelAlbum;
use PHPUnit_Framework_TestCase;
/**
* @category Module
* @package Album
* @subpackage UnitTests
* @group Module_Album
*/
class AlbumTest extends PHPUnit_Framework_TestCase {
public function testAlbumInitialState() {
$album = new Album();
$this->assertNull($album->artist, '"artist" should initially be null');
$this->assertNull($album->id, '"id" should initially be null');
$this->assertNull($album->title, '"title" should initially be null');
}
public function testExchangeArraySetsPropertiesCorrectly() {
$album = new Album();
$data = array('artist' => 'some artist',
'id' => 123,
'title' => 'some title');
$album->exchangeArray($data);
$this->assertSame($data['artist'], $album->artist, '"artist" was not set correctly');
$this->assertSame($data['id'], $album->id, '"id" was not set correctly');
$this->assertSame($data['title'], $album->title, '"title" was not set correctly');
}
public function testExchangeArraySetsPropertiesToNullIfKeysAreNotPresent() {
$album = new Album();
$album->exchangeArray(array('artist' => 'some artist',
'id' => 123,
'title' => 'some title'));
$album->exchangeArray(array());
$this->assertNull($album->artist, '"artist" should have defaulted to null');
$this->assertNull($album->id, '"id" should have defaulted to null');
$this->assertNull($album->title, '"title" should have defaulted to null');
}
public function testGetArrayCopyReturnsAnArrayWithPropertyValues() {
$album = new Album();
$data = array('artist' => 'some artist',
'id' => 123,
'title' => 'some title');
$album->exchangeArray($data);
$copyArray = $album->getArrayCopy();
$this->assertSame($data['artist'], $copyArray['artist'], '"artist" was not set correctly');
$this->assertSame($data['id'], $copyArray['id'], '"id" was not set correctly');
$this->assertSame($data['title'], $copyArray['title'], '"title" was not set correctly');
}
public function testInputFiltersAreSetCorrectly() {
$album = new Album();
$inputFilter = $album->getInputFilter();
$this->assertSame(3, $inputFilter->count());
$this->assertTrue($inputFilter->has('artist'));
$this->assertTrue($inputFilter->has('id'));
$this->assertTrue($inputFilter->has('title'));
}
}
NetBeans知道在哪里可以找到phpunit二进制文件:(我想把图片贴在这里,虽然没有名气:),我会尽力解释的)要配置的路径在options - php - unit testing
中/usr/local/pear/bin/phpunit
在项目的属性中,我设置了"使用XML配置",并将路径粘贴到phpunit.xml。我还检查了"在运行测试之前请求测试组"-我在组"Module_Album"上面给出了测试-这样我确保PHPUnit找到了正确的测试。
我右键单击项目,选择"测试",它显示了我的组"Module_Album",我检查它并点击"确定"。它运行了一些东西,告诉我它找到了正确的phpunit.xml("从FULL_PATH/tests/phpunit.xml读取配置")。在运行之后,它告诉我,它没有执行任何测试。
这是NetBeans的完整输出:
PHPUnit 3.7.19 by Sebastian Bergmann.
Configuration read from FULL_PATH/tests/phpunit.xml
Time: 9 seconds, Memory: 348.25Mb
[2KNo tests executed!
[2K
Generating code coverage report in Clover XML format ... done
无论如何,我可以通过shell(终端)成功地做同样的事情。如果我直接挂载测试目录并运行phpunit,使用phpunit二进制文件的完整路径(确保我没有使用不同的版本),指定phpunit.xml的完整路径,等等,这都没有关系。以下是一些示例:
phpunit
phpunit -c FULL_PATH/tests/phpunit.xml
/usr/local/pear/bin/phpunit -c FULL_PATH/tests/phpunit.xml
所有这些命令给了我,我所期望的:
PHPUnit 3.7.19 by Sebastian Bergmann.
Configuration read from FULL_PATH/tests/phpunit.xml
.....
Time: 0 seconds, Memory: 12.75Mb
OK (5 tests, 16 assertions)
- 我不明白,为什么这在shell中工作,而不是通过NetBeans
- NetBeans使用350MB,而在shell中它只使用12.75MB
- 如果我删除了"在运行测试之前询问测试组"选项,它似乎会尝试运行所有ZendTests。不知道这是怎么可能的,从phpunt .xml中应该找不到它们。
任何帮助都是感激的,谢谢!
我终于弄明白是怎么回事了。我以前用phpunit.xml运行了ZendTests,它位于vendor/zendframework/zendframework/tests
由于某种原因,如果选择不同的phpunit.xml, NetBeans将保存testfile目录,它在第一次测试运行时找到该目录,并且不会释放它们。我必须通过"右键单击项目"-"属性"-"源代码"-"测试文件夹"来更改"新"测试目录的路径。
现在NetBeans运行测试并给出与CLI完全相同的输出。