我似乎在Zend Framework 2中为自己的模块执行PHPUnit测试时遇到了一些问题。
操作系统:Mac OS 10.8.3
Zend Framework 2.1.4
PHPUnit版本3.7.19(通过梨安装)
PHP版本5.3.15(启用xdebug,版本2.1.3)
我按照Zend指南的说明,创建了“专辑”模块(http://framework.zend.com/manual/2.1/en/user-guide/modules.html)
我希望将它们全部放在一个集中文件夹中,而不是将单元测试放入模块文件夹中。这是我的应用程序的基本结构:
-config
-data
- 模
-public
-tests
--modules
---专辑
---- AlbumTest.php
--Bootstrap.php
--phpunit.xml
-vendor
...(许可证,自述文件,作曲家和init_autoloader.php)
- > Module Album的测试位于“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';
Zend\Mvc\Application::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 AlbumTest\Model;
use Album\Model\Album;
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二进制文件:
(我想在这里发布图片,虽然没有名声:),我会尽力解释一下)
在选项中配置的路径 - php - 单元测试
/ usr / local / pear / bin / phpunit
和
的/ usr /本地/梨/ bin中/ PHPUnit的-skelgen
在项目的属性中,我设置了“使用XML配置”并将路径粘贴到phpunit.xml。我还检查了“在运行测试之前询问测试组” - 我将测试放在“Module_Album”组之上 - 我确保这样,PHPUnit找到了正确的测试。
我右键单击Project并选择“Test”,它显示组“Module_Album”,我检查它并单击“Ok”。它运行一些东西,告诉我它找到了正确的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(终端)成功地做到这一点。没关系,如果我直接挂载tests目录并运行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)
感谢任何帮助,谢谢!
答案 0 :(得分:0)
我终于想通了,发生了什么事。我之前使用phpunit.xml运行了ZendTests,这是在vendor / zendframework / zendframework / tests中
出于某种原因,如果选择了不同的phpunit.xml,NetBeans将保存testfile目录,该目录在第一次测试运行中找到并且不会释放它们。我必须通过“右键单击项目” - “属性” - “源” - “测试文件夹”来更改“新”测试目录的路径。
现在NetBeans运行测试并提供与CLI相同的输出。