Codeigniter API 调用的 PHPUnit/Xdebug 代码覆盖率
我们正在使用 Codeigniter,并且有 2 个选项来调用我们的 API 控制器:
- 我们可以使用通过 Curl 调用控制器 url 的客户端,
- 我们可以使用从命令行调用控制器的客户端。
这对于我们网站的功能来说非常合适。但是,当我运行 PHPUnit 时,控制器的覆盖率报告为空,而所有模型的覆盖率报告都是正确的。
在跟踪 xdebug 如何创建报告时,似乎使用基于 Curl 的客户端或 CLI 客户端是在测试函数的范围之外调用的,因此 xdebug_get_code_coverage() 不会跟踪执行的控制器代码。
在这种情况下是否可以配置 xdebug 来识别代码覆盖率?是否可以在 PHPUnit 测试函数的范围内调用 Codeigniter 控制器?还有其他可能的解决方案吗?
We are using Codeigniter and have 2 options to call our API controllers:
- we can use a client that calls the controller's url through Curl,
- we can use a client that calls the controller from the command line.
This is perfectly fine for the functionality of our site. However, when I run PHPUnit, the coverage reports for the Controllers are blank while the coverage reports for all Models are correct.
In tracing how xdebug creates the reports, it appears that using the Curl-based client or the CLI client are called outside of the scope of the test function, so xdebug_get_code_coverage() does not track the controller code that is executed.
Is it possible to configure xdebug to recognize code coverage in this scenario? Is it possible to call Codeigniter controllers within the scope of the PHPUnit test function? Any other possible solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这很容易实现。请参阅http://www.phpunit.de/manual/current/en/selenium。 html 了解更多信息
基本上,您将一些特殊文件放入您的网络根目录中:
当使用 GET 参数
PHPUNIT_SELENIUM_TEST_ID
运行 URL 时,覆盖率信息将被跟踪,并且 PHPUnit 可以通过请求coverageScriptUrl
来收集它。Yes, that's easily possible. See http://www.phpunit.de/manual/current/en/selenium.html for more information about it
Basically you put some special files in your web root:
When running a URL with the GET parameter
PHPUNIT_SELENIUM_TEST_ID
, the coverage information gets tracked and PHPUnit can collect it by requesting thecoverageScriptUrl
.另一种选择:请参阅我们的 SD PHP 测试覆盖率 工具。
它不使用 xdebug 来收集覆盖率数据,因此不会出现 xdebug 特有的问题。它使用脚本来收集测试覆盖率数据;一旦检测完毕,无论脚本如何执行,您都将获得测试覆盖率数据。
(检测是临时的;一旦你有了检测代码,你就扔掉检测过的代码
收集的测试覆盖率数据,因此不会影响您的生产代码库)。
这种方法确实需要您显式列出您想要覆盖率数据的所有 PHP 脚本;如果你愿意的话,你可以忽略一些。通常这不值得费心;大多数用户只是简单地列出所有 PHP 脚本。
An alternative: see our SD PHP Test Coverage tool.
It doesn't use xdebug to collect coverage data, so it won't have xdebug's specific problems. It instruments a script to collect test coverage data; once instrumented, no matter how the script is executed, you will get test coverage data.
(The instrumentation is temporary; you throw the instrumented code away once you have
the test coverage data collected, so it doesn't affect your production code base).
This approach does require you to explicitly list all PHP scripts for which you want coverage data; you can ignore some if you want. Usually it isn't worth the bother; most of the users simply list all PHP scripts.