Codeigniter API 调用的 PHPUnit/Xdebug 代码覆盖率

发布于 2024-10-26 01:26:13 字数 412 浏览 12 评论 0原文

我们正在使用 Codeigniter,并且有 2 个选项来调用我们的 API 控制器:

  1. 我们可以使用通过 Curl 调用控制器 url 的客户端,
  2. 我们可以使用从命令行调用控制器的客户端。

这对于我们网站的功能来说非常合适。但是,当我运行 PHPUnit 时,控制器的覆盖率报告为空,而所有模型的覆盖率报告都是正确的。

在跟踪 xdebug 如何创建报告时,似乎使用基于 Curl 的客户端或 CLI 客户端是在测试函数的范围之外调用的,因此 xdebug_get_code_coverage() 不会跟踪执行的控制器代码。

在这种情况下是否可以配置 xdebug 来识别代码覆盖率?是否可以在 PHPUnit 测试函数的范围内调用 Codeigniter 控制器?还有其他可能的解决方案吗?

We are using Codeigniter and have 2 options to call our API controllers:

  1. we can use a client that calls the controller's url through Curl,
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假扮的天使 2024-11-02 01:26:13

是的,这很容易实现。请参阅http://www.phpunit.de/manual/current/en/selenium。 html 了解更多信息

基本上,您将一些特殊文件放入您的网络根目录中:

PHPUnit_Extensions_SeleniumTestCase 可以收集通过 Selenium 运行的测试的代码覆盖率信息:
PHPUnit/Extensions/SeleniumTestCase/phpunit_coverage.php 复制到 Web 服务器的文档根目录中。
在网络服务器的 php.ini 配置文件中,将 PHPUnit/Extensions/SeleniumTestCase/prepend.phpPHPUnit/Extensions/SeleniumTestCase/append.php 配置为 auto_prepend_file<分别是 /code> 和 auto_append_file
在扩展 PHPUnit_Extensions_SeleniumTestCase 的测试用例类中,使用 protected
$coverageScriptUrl = 'http://host/phpunit_coverage.php';
配置 phpunit_coverage.php 脚本的 URL。


当使用 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:

PHPUnit_Extensions_SeleniumTestCase can collect code coverage information for tests run through Selenium:
Copy PHPUnit/Extensions/SeleniumTestCase/phpunit_coverage.php into your webserver's document root directory.
In your webserver's php.ini configuration file, configure PHPUnit/Extensions/SeleniumTestCase/prepend.php and PHPUnit/Extensions/SeleniumTestCase/append.php as the auto_prepend_file and auto_append_file, respectively.
In your test case class that extends PHPUnit_Extensions_SeleniumTestCase, use protected
$coverageScriptUrl = 'http://host/phpunit_coverage.php';
to configure the URL for the phpunit_coverage.php script.

When running a URL with the GET parameter PHPUNIT_SELENIUM_TEST_ID, the coverage information gets tracked and PHPUnit can collect it by requesting the coverageScriptUrl.

独孤求败 2024-11-02 01:26:13

另一种选择:请参阅我们的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文