我目前正在尝试获取基于
Yii framework的程序的代码覆盖率报告.
代码覆盖率由PHPUnit 3.6生成,我使用whitelist approach来源文件过滤.
问题是,当我设置addUncoveredFilesFromWhitelist =“true”选项时,代码覆盖率会突然出现以下错误:
Generating code coverage report,this may take a moment.PHP Warning: include(CButtonColumn.PHP): Failed to open stream: No such file or directory in /home/hijarian/systems/yii/framework/YiiBase.PHP on line 418 PHP Stack trace: PHP 1. {main}() /usr/bin/PHPunit:0 PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/PHPunit:46 PHP 3. PHPUnit_TextUI_Command->run() /usr/share/PHP/PHPUnit/TextUI/Command.PHP:125 PHP 4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/PHP/PHPUnit/TextUI/Command.PHP:187 PHP 5. PHP_CodeCoverage_Report_HTML->process() /usr/share/PHP/PHPUnit/TextUI/TestRunner.PHP:373 PHP 6. PHP_CodeCoverage->getReport() /usr/share/PHP/PHP/CodeCoverage/Report/HTML.PHP:133 PHP 7. PHP_CodeCoverage_Report_Factory->create() /usr/share/PHP/PHP/CodeCoverage.PHP:141 PHP 8. PHP_CodeCoverage->getData() /usr/share/PHP/PHP/CodeCoverage/Report/Factory.PHP:65 PHP 9. PHP_CodeCoverage->processUncoveredFilesFromWhitelist() /usr/share/PHP/PHP/CodeCoverage.PHP:173 PHP 10. include_once() /usr/share/PHP/PHP/CodeCoverage.PHP:516 PHP 11. YiiBase::autoload() /home/hijarian/systems/yii/framework/YiiBase.PHP:0 PHP Warning: include(): Failed opening 'CButtonColumn.PHP' for inclusion (include_path='.:/home/hijarian/projects/lexgarant/webapp/protected/components:/home/hijarian/projects/lexgarant/webapp/protected/models:/usr/share/PHP:/usr/share/pear') in /home/hijarian/systems/yii/framework/YiiBase.PHP on line 418 PHP Stack trace: PHP 1. {main}() /usr/bin/PHPunit:0 PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/PHPunit:46 PHP 3. PHPUnit_TextUI_Command->run() /usr/share/PHP/PHPUnit/TextUI/Command.PHP:125 PHP 4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/PHP/PHPUnit/TextUI/Command.PHP:187 PHP 5. PHP_CodeCoverage_Report_HTML->process() /usr/share/PHP/PHPUnit/TextUI/TestRunner.PHP:373 PHP 6. PHP_CodeCoverage->getReport() /usr/share/PHP/PHP/CodeCoverage/Report/HTML.PHP:133 PHP 7. PHP_CodeCoverage_Report_Factory->create() /usr/share/PHP/PHP/CodeCoverage.PHP:141 PHP 8. PHP_CodeCoverage->getData() /usr/share/PHP/PHP/CodeCoverage/Report/Factory.PHP:65 PHP 9. PHP_CodeCoverage->processUncoveredFilesFromWhitelist() /usr/share/PHP/PHP/CodeCoverage.PHP:173 PHP 10. include_once() /usr/share/PHP/PHP/CodeCoverage.PHP:516 PHP 11. YiiBase::autoload() /home/hijarian/systems/yii/framework/YiiBase.PHP:0 PHP Fatal error: Class 'CButtonColumn' not found in /home/hijarian/projects/lexgarant/webapp/protected/components/CLexgarantButtonColumn.PHP on line 4 PHP Stack trace: PHP 1. {main}() /usr/bin/PHPunit:0 PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/PHPunit:46 PHP 3. PHPUnit_TextUI_Command->run() /usr/share/PHP/PHPUnit/TextUI/Command.PHP:125 PHP 4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/PHP/PHPUnit/TextUI/Command.PHP:187 PHP 5. PHP_CodeCoverage_Report_HTML->process() /usr/share/PHP/PHPUnit/TextUI/TestRunner.PHP:373 PHP 6. PHP_CodeCoverage->getReport() /usr/share/PHP/PHP/CodeCoverage/Report/HTML.PHP:133 PHP 7. PHP_CodeCoverage_Report_Factory->create() /usr/share/PHP/PHP/CodeCoverage.PHP:141 PHP 8. PHP_CodeCoverage->getData() /usr/share/PHP/PHP/CodeCoverage/Report/Factory.PHP:65 PHP 9. PHP_CodeCoverage->processUncoveredFilesFromWhitelist() /usr/share/PHP/PHP/CodeCoverage.PHP:173 PHP 10. include_once() /usr/share/PHP/PHP/CodeCoverage.PHP:516
(它本身很有趣,为什么有三个几乎相同的错误而不只是一个.)当addUncoveredFilesFromWhitelist =“false”代码覆盖成功生成时,当然,没有测试未涵盖的文件.
Yii框架的文件放在$HOME / systems / yii /目录中.
应用程序我试图覆盖$HOME / projects / $APPNAME /.
这是我的PHPunit.xml:
<PHPunit bootstrap="bootstrap.PHP" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" stopOnFailure="false"> <filter> <!-- DO NOT SET TO "true" CODE COVERAGE WILL BREAK --> <whitelist addUncoveredFilesFromWhitelist="false"> <directory suffix=".PHP">..</directory> <exclude> <directory suffix=".PHP">../config/</directory> <directory suffix=".PHP">../messages/</directory> <directory suffix=".PHP">../tests/</directory> <directory suffix=".PHP">../runtime/</directory> <directory suffix=".PHP">/home/hijarian/systems/yii/framework/</directory> </exclude> </whitelist> </filter> <logging> <log type="coverage-html" target="report/html" title="Lexgarant Online" charset="UTF-8" yui="true" highlight="true" lowUpperBound="35" highLowerBound="70" /> </logging> <testsuites> <testsuite name="All_Unit_Tests"> <directory>./unit/models/</directory> <directory>./unit/logic/</directory> <directory>./unit/components/</directory> </testsuite> </testsuites> <selenium> <browser name="Firefox" browser="*firefox" /> </selenium> </PHPunit>
我将所有测试放在应用程序目录中的protected / tests目录下,如Yii权威指南Test Environment Setup中所述.
cd $APPDIR/protected/tests/ PHPunit
单元测试本身顺利进行,只有代码覆盖率中断.
也许我应该设置排除列表以排除Yii类的目录,但我有点害怕这种经历,因为有几十个文件夹,Yii是一个高度模块化的系统.当然,如果它是唯一的解决方案……
编辑:所以,好奇的是,完整的解决方案看起来像这样:
PHPunit.xml
<PHP> <!-- HACK --> <includePath>YIIPATH/framework/zii/widgets/grid</includePath> </PHP> <filter> <whitelist addUncoveredFilesFromWhitelist="true"> <directory suffix=".PHP">../models/</directory> <directory suffix=".PHP">../components/</directory> <!-- any other directories you want to check --> </whitelist> </filter>
只需将YIIPATH占位符替换为Yii框架的真实路径即可.
它肯定与CButtonColumn有关,但是现在我只是使用这个带有includePath的hack并对此感到高兴.删除hack会使整个bug变得活跃起来.