首页 > 解决方案 > Laravel 不生成代码覆盖率报告

问题描述

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

php artisan test --coverage-html reports

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

 <logging>
    <log type="coverage-html" target="tests/coverage" showUncoveredFiles="true"/>
    <log type="coverage-clover" target="build/logs/clover.xml"/>
  </logging>

那是行不通的。我也试过

    <coverage processUncoveredFiles="true">
      <include>
        <directory suffix=".php">./app</directory>
      </include>
      <report>
        <html outputDirectory="tests/reports/coverage"/>
      </report>
    </coverage>

依然没有; 下面是我的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>

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

标签: phplaravelphpunitlaravel-testing

解决方案


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

php -dxdebug.mode=coverage vendor/bin/phpunit --coverage-clover='reports/coverage/coverage.xml' --coverage-html='reports/coverage'

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


推荐阅读