如何将未覆盖的文件添加到 Yii 应用程序的 PHPUnit 代码覆盖率报告中
我目前正在尝试获取基于 Yii 框架 的 PHP 应用程序的代码覆盖率报告。
代码覆盖率由 PHPUnit 3.6 生成,我正在使用 白名单方法进行源文件过滤。
问题是,当我设置选项 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
这本身很有趣,为什么会出现三个几乎相同的错误,而不是只有一个。)当 < code>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 权威指南中的测试环境设置。
PHPUnit 的调用方式如下:
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
一起使用,并且对此感到高兴。删除这个黑客让整个错误变得活跃。
I am currently trying to get the code coverage report for a PHP application based on Yii framework.
Code coverage gets generated by PHPUnit 3.6, and I am using whitelist approach to source files filtering.
Problem is, when I set the option addUncoveredFilesFromWhitelist="true"
, code coverage breaks with the following error:
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
(It's interesting by itself, why there is three almost identical errors instead of just one.) When addUncoveredFilesFromWhitelist="false"
code coverage generates successfully, but, of course, without the files not covered by tests.
Files of the Yii framework placed in the $HOME/systems/yii/
directory.
Application I'm trying to cover placed in the $HOME/projects/$APPNAME/
.
Here's my 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>
I have all the tests placed under the protected/tests
directory in the application's catalogue, as described in the Test Environment Setup in The Definitive Guide to Yii.
PHPUnit is invoked as follows:
cd $APPDIR/protected/tests/
phpunit
The unit testing itself goes smoothly, only code coverage breaks.
Maybe I should set up the exlude list to exclude directories with Yii's classes, but I am somewhat afraid of this experience because there's dozens of folders, Yii is a highly modular system. Of course, if it's an only solution...
EDIT: So, for curious about it, full solution looks like this:
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>
Just replace YIIPATH placeholder with real path to Yii framework.
It definitely has something to do with CButtonColumn
, but for now I'm just using this hack with includePath
and happy about it. Removing the hack brings this whole bug alive.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么会发生这种情况:
如果您告诉 PHPUnit 执行
addUncoveredFilesFromWhitelist="true 操作,PHPUnit 将
。require
/include
白名单中的所有文件“如果这些文件包含可执行代码,它将被执行,并且您将看到所出现问题的结果。
该怎么办
这有点麻烦,但它有效
我不知道为什么会弹出这些错误,但似乎是包含路径问题吗?我想说,框架应该有一种解决方法。
Why this is happening:
PHPUnit will
require
/include
all files in the white-listed if you tell it doaddUncoveredFilesFromWhitelist="true"
.If those files contain executable code it will be executed and you are seeing the results of the arising issues.
What to do
It's a little trouble but it works
I have no real clue as to WHY those errors pop up but it seems to be an include path issue? The framework should have a way of resolving that I'd say.
最白的代码覆盖率报告应仅包含您正在测试的文件。假设您没有为 Yii 框架本身编写测试,请将其从白色中删除。这不仅有望解决问题,而且报告的构建速度也会快。
The whitest and code coverage report should include only the files you are testing. Assuming you aren't writing tests for the Yii framework itself, remove it from the whitest. Not only will this hopefully solve the problem, but the report will build much faster.