【问题标题】:How to deal with unit-tests in Zend Framework 2?如何处理 Zend Framework 2 中的单元测试?
【发布时间】:2014-12-29 23:51:27
【问题描述】:

我有一个 Zend-Framework 2 项目,但在组织我的单元测试时遇到了问题。我的单元测试位于每个模块的一个“测试”文件夹中。如果我在模块的 test 文件夹中使用 phpunit 从命令行运行测试,测试将成功运行。

例子:

c:\project\module\AccessControl\test>phpunit

PHPUnit 3.7.14 by Sebastian Bergmann.
Configuration read from c:\project\module\AccessControl\test\phpunit.xml
........Time: 0 seconds, Memory: 8.25Mb
OK (8 tests, 10 assertions)

问题:这里只有一个模块是 testet。但我想从文件夹 c:\project\ 中运行一个测试套件来测试洞项目。 我在那里放了一个 bootstrap.php 和一个 phpunit.xml。

我从命令行运行它:

C:\project\tests>phpunit --debug --configuration phpunit.xml

PHPUnit 3.7.14 by Sebastian Bergmann.
Configuration read from C:\project\tests\phpunit.xml
Starting test 'AccessControlTest\Model\AuthenticationTest::testInitDefault'.
PHP Fatal error:  Class 'AccessControlTest\Bootstrap' not found in C:\project\module\AccessControl\test\AccessControlTest\Model\AuthenticationTest.php on line 22
PHP Stack trace:
PHP   1. {main}() C:\Program Files (x86)\PHP\phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() C:\Program Files (x86)\PHP\phpunit:46
PHP   3. PHPUnit_TextUI_Command->run() C:\Program Files (x86)\PHP\PEAR\PHPUnit\TextUI\Command.php:129
PHP   4. PHPUnit_TextUI_TestRunner->doRun() C:\Program Files (x86)\PHP\PEAR\PHPUnit\TextUI\Command.php:176
PHP   5. PHPUnit_Framework_TestSuite->run() C:\Program Files (x86)\PHP\PEAR\PHPUnit\TextUI\TestRunner.php:346
PHP   6. PHPUnit_Framework_TestSuite->run() C:\Program Files (x86)\PHP\PEAR\PHPUnit\Framework\TestSuite.php:705
PHP   7. PHPUnit_Framework_TestSuite->runTest() C:\Program Files (x86)\PHP\PEAR\PHPUnit\Framework\TestSuite.php:745
PHP   8. PHPUnit_Framework_TestCase->run() C:\Program Files (x86)\PHP\PEAR\PHPUnit\Framework\TestSuite.php:775
PHP   9. PHPUnit_Framework_TestResult->run() C:\Program Files (x86)\PHP\PEAR\PHPUnit\Framework\TestCase.php:769
PHP  10. PHPUnit_Framework_TestCase->runBare() C:\Program Files (x86)\PHP\PEAR\PHPUnit\Framework\TestResult.php:648
PHP  11. AccessControlTest\Model\AuthenticationTest->setUp() C:\Program Files (x86)\PHP\PEAR\PHPUnit\Framework\TestCase.php:821

文件 phpunit.xml 如下所示:

<phpunit bootstrap="./Bootstrap.php" colors="true" verbose="true">
 <testsuite name="SgtrCatalogTestsuite">
  <directory>../module/AccessControl</directory>
 </testsuite>
</phpunit>

看起来有些模块路径不正确。有人可以帮忙告诉我如何设置吗?

编辑 期待您的回复!!!

与此同时,我发现了这个: How to consolidate ZF2 unit/application module tests into a single call?

似乎不可能从根级别运行所有模块测试。我现在尝试使用它并让你知道它是否有效:https://github.com/prolic/HumusPHPUnitModule

【问题讨论】:

  • 你的 composer.json 中有自动加载功能吗?
  • 你的 Bootstrap.php 是什么样的?
  • ty,自动加载器在那里......我编辑了这个问题,因为我发现了一个可能的重复项

标签: unit-testing zend-framework2 phpunit


【解决方案1】:

尝试在您的 composer.json 中添加此部分

"autoload": {
        "psr-0": {
            "AccessControlTest\\": "tests/",
            "AccessControl\\": "src/module/AccessControl/src/"
        }
    }

然后运行

$ php composer.phar dump-autoload

【讨论】:

  • 我们试试这个,但它对我们不起作用。我们找到了解决问题的方法。我编辑一下,谢谢
猜你喜欢
  • 2012-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-19
  • 2013-01-11
  • 2018-10-31
  • 1970-01-01
  • 2013-09-18
相关资源
最近更新 更多