Laravel 不生成代码覆盖率报告

Owe*_*vin 11 php phpunit laravel laravel-testing

我正在使用 Laravel 8 并测试我的应用程序,我正在运行

php artisan test --coverage-html reports
Run Code Online (Sandbox Code Playgroud)

测试运行成功。问题是没有生成覆盖率报告。我已经在这里查看了答案,据说解决方案将以下内容添加到我的phpunit.xml

 <logging>
    <log type="coverage-html" target="tests/coverage" showUncoveredFiles="true"/>
    <log type="coverage-clover" target="build/logs/clover.xml"/>
  </logging>
Run Code Online (Sandbox Code Playgroud)

那是行不通的。我也尝试过

    <coverage processUncoveredFiles="true">
      <include>
        <directory suffix=".php">./app</directory>
      </include>
      <report>
        <html outputDirectory="tests/reports/coverage"/>
      </report>
    </coverage>
Run Code Online (Sandbox Code Playgroud)

依然没有; 下面是我的phpunit.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
  <testsuites>
    <testsuite name="Unit">
      <directory suffix="Test.php">./tests/Unit</directory>
      <directory suffix="Test.php">./packages/okotieno</directory>
    </testsuite>

    <testsuite name="Feature">
      <directory suffix="Test.php">./tests/Feature</directory>
    </testsuite>
  </testsuites>
  <filter>
    <whitelist processUncoveredFilesFromWhitelist="true">
      <directory suffix=".php">./app</directory>
    </whitelist>
  </filter>
  <logging>
    <log type="coverage-html" target="tests/coverage" showUncoveredFiles="true"/>
    <log type="coverage-clover" target="build/logs/clover.xml"/>
  </logging>
  <php>
    <server name="APP_ENV" value="testing"/>
    <server name="BCRYPT_ROUNDS" value="4"/>
    <server name="CACHE_DRIVER" value="array"/>
    <server name="MAIL_DRIVER" value="array"/>
    <server name="QUEUE_CONNECTION" value="sync"/>
    <server name="SESSION_DRIVER" value="array"/>
    <env name="DB_DATABASE" value="testing_db"/>
  </php>

</phpunit>

Run Code Online (Sandbox Code Playgroud)

我还跑去php artisan test --help看了有效的标志,--coverage-html不在列表中,但它运行了。我怎样才能解决这个问题?

小智 7

我有同样的问题。这是由于php.ini文件中xdebug的配置导致的。就我而言,我有 xdebug 3.0.4。您可以使用 php -v 来检查它。

您必须在 php.ini 文件中包含以下行:

[XDebug]
zend_extension = "c:\xampp8\php\ext\php_xdebug-3.0.4-7.4-vc15-x86_64.dll"
xdebug.mode=debug,coverage
xdebug.client_host=127.0.0.1
xdebug.client_port=9000
Run Code Online (Sandbox Code Playgroud)


Mhm*_*hmd 6

首先,你需要确保你已经安装了Xdebug,然后你需要确保这个Xdebug处于覆盖模式,你可以通过下面的命令运行它(这使得它在运行过程中打开)

php -dxdebug.mode=覆盖供应商/bin/phpunit --coverage-clover='reports/coverage/coverage.xml' --coverage-html='reports/coverage'

请考虑 Xdebug 不是为生成报告而设计的,因此,如果您想为大量测试生成报告,您将面临操作系统的进程终止问题,在这种情况下,您应该寻找其他软件包


小智 6

确保 xdebug 已安装并在 php ini 中配置。

如果安装了 php -v 将显示以下结果以及 xdebug 详细信息

PHP 8.0.15 (cli) (built: Jan 21 2022 04:49:41) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.15, Copyright (c) Zend Technologies
   with Xdebug v3.1.3, Copyright (c) 2002-2022, by Derick Rethans
   with Zend OPcache v8.0.15, Copyright (c), by Zend Technologies
Run Code Online (Sandbox Code Playgroud)

确保在 php ini 文件中启用 xdebug 覆盖模式

[debug]
zend_extension=/usr/local/Cellar/php@8.0/8.0.15/pecl/20200930/xdebug.so
xdebug.mode=coverage,debug
xdebug.start_with_request=yes
xdebug.client_port=9003
xdebug.log_level=7
xdebug.idekey=VSCODE
xdebug.client_host=127.0.0.1
Run Code Online (Sandbox Code Playgroud)

在 phpunint.xml 文件中添加 without 作为直接子项

<coverage processUncoveredFiles="true">
   <include>
     <directory suffix=".php">./app</directory>
   </include>
</coverage>
Run Code Online (Sandbox Code Playgroud)

使用 artisan 通过路径运行测试以获取代码覆盖率报告

php artisan test --coverage-html tests/reports/coverage
Run Code Online (Sandbox Code Playgroud)