如何处理Zend Framework 2中的单元测试?

时间:2014-11-03 10:30:40

标签: unit-testing zend-framework2 phpunit

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

示例:

C:\项目\模块\ AccessControl的\试验>的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>

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

修改 Ty给你回复!!!

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

似乎无法从rootlevel运行所有模块测试。我现在尝试使用它,让你知道它是否有效:https://github.com/prolic/HumusPHPUnitModule

1 个答案:

答案 0 :(得分:0)

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

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

然后运行

$ php composer.phar dump-autoload