【问题标题】:PHPUnit can't generate code coverage when running tests in separate processes在单独的进程中运行测试时,PHPUnit 无法生成代码覆盖率
【发布时间】:2023-03-21 08:54:01
【问题描述】:

我有一个问题,我在 Google 上找不到解决方案。 当我使用 PHPUnit 和 Xdebug 运行单元测试时,我想做一个代码覆盖。

但是,当我这样做时,我得到了这个错误:

PHPUnit\Framework\Exception: Fatal error: Uncaught SebastianBergmann\CodeCoverage\NoCodeCoverageDriverAvailableException: No code coverage driver available in /Users/theobenoit/Desktop/Antipode.nosync/Sites/wp-framework-core/vendor/phpunit/php-code-coverage/src/Driver/Selector.php:53
   ├ Stack trace:
   ├ #0 Standard input code(43): SebastianBergmann\CodeCoverage\Driver\Selector->forLineCoverage(Object(SebastianBergmann\CodeCoverage\Filter))
   ├ #1 Standard input code(123): __phpunit_run_isolated_test()
   ├ #2 {main}
   ├   thrown in /Users/theobenoit/Desktop/Antipode.nosync/Sites/wp-framework-core/vendor/phpunit/php-code-coverage/src/Driver/Selector.php on line 53

我意识到这是因为在我的一些测试中,顶部有这个:

/**
 * @runTestsInSeparateProcesses
 * @preserveGlobalState disabled
 */

当我删除 @runTestsInSeparateProcesses 时,一切正常,但由于我模拟函数而出现问题。

如果有人遇到过这个问题。

【问题讨论】:

    标签: php unit-testing phpunit xdebug


    【解决方案1】:

    这可能是因为使用runTestsInSeparateProcesses,PHPUnit 会为每个测试启动一个新的 PHP 进程。而且它似乎在没有加载 Xdebug 的情况下这样做。

    为每个测试启动的 PHP 必须与您最初启动 PHPUnit 时使用的 PHP 相同。也许PATH中首先有一个不同的PHP二进制文件?

    如果是这种情况,解决方法是确保“正确”的 PHP 在 PATH 中排在第一位。

    【讨论】:

    • 您好@Derick,感谢您的回答。是的,你可能是对的。可能是因为PHP不一样。当我运行 php -v 时,我有带有 xDebug 的 php,但是当我运行哪个 php 时,我得到了 php 的路径,而这个 php -v 没有 xDebug。 php:别名为 /Applications/MAMP/bin/php/php7.4.9/bin/php -c "/Library/Application Support/appsolute/MAMP PRO/conf/php7.4.9.ini"
    • 顺便说一句,如果我只运行 ./vendor/bin/phpunit 我有“没有代码驱动程序”但如果我执行 php ./vendor/bin/phpunit 就可以了。它可能来自这里
    【解决方案2】:

    我可以通过在没有映射时禁用强制中断来解决同样的问题,这是 IDE 中 xdebug-settings 下的一个设置。

    见: https://intellij-support.jetbrains.com/hc/en-us/community/posts/360009770579-Docker-debug-problem-Remote-file-path-home-www-Standard-input-code-is-not-mapped-to-any-file-path-in-project-

    【讨论】:

      猜你喜欢
      • 2012-02-24
      • 2012-01-18
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-30
      • 1970-01-01
      • 2011-03-09
      相关资源
      最近更新 更多