【问题标题】:ZendFramework 2 - PHPUnit - command line works, NetBeans states "No tests executed"ZendFramework 2 - PHPUnit - 命令行工作,NetBeans 状态“未执行测试”
【发布时间】:2013-04-10 04:55:04
【问题描述】:

我在 Zend Framework 2 中为自己的模块执行 PHPUnit 测试时似乎有点问题。

操作系统:Mac OS 10.8.3
Zend 框架 2.1.4
PHPUnit 版本 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_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>

相册测试.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/local/pear/bin/phpunit-skelgen

在项目的属性中,我设置了“使用 XML 配置”并将路径粘贴到 phpunit.xml。我还检查了“在运行测试之前询问测试组”——我在“Module_Album”组上方进行了测试——这样我确保 PHPUnit 找到了正确的测试。

我右键单击项目并选择“测试”,它显示组“模块_专辑”,我检查它并单击“确定”。它运行一些东西,告诉我它找到了正确的 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
  • 如果我删除了“在运行测试之前询问测试组”选项,它似乎会尝试运行所有 ZendTest。不知道这怎么可能,从 phpunit.xml 应该找不到它们。

感谢任何帮助,谢谢!

【问题讨论】:

    标签: phpunit zend-framework2 netbeans-7.3


    【解决方案1】:

    我终于明白了,这是怎么回事。我之前使用 phpunit.xml 运行 ZendTests,它位于 vendor/zendframework/zendframework/tests
    出于某种原因,NetBeans 保存了 testfile 目录,如果选择了不同的 phpunit.xml,它会在第一次测试运行中找到它并且不会释放它们。我必须通过“右键单击项目”-“属性”-“源”-“测试文件夹”来更改“新”测试目录的路径。

    现在 NetBeans 运行测试并提供与 CLI 完全相同的输出。

    【讨论】:

      猜你喜欢
      • 2013-07-17
      • 1970-01-01
      • 2018-05-05
      • 1970-01-01
      • 2013-11-04
      • 2015-06-15
      • 2018-10-08
      • 2012-04-24
      • 2012-08-31
      相关资源
      最近更新 更多