【问题标题】:How to get a simple phpunit codecoverage summary in text output?如何在文本输出中获得简单的 phpunit 代码覆盖率摘要?
【发布时间】:2021-10-05 22:18:16
【问题描述】:

我在 php 8.0.3 上使用 PHPUnit 9.5.7 我想要一个检查器作为 git 钩子来检查行的最小代码覆盖率。

我在在线示例中看到一个简单的 3 行输出作为运行测试后的摘要。我如何获得这个输出?我搜索了几篇文章和配置文档,但没有找到任何东西。

我的目标是如果没有达到最低限度的行覆盖率,则拒绝提交。因此,如果您有其他想法来完成这项工作,我很乐意。

这是我当前的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
    backupGlobals="false"
    colors="true"
    bootstrap="tests/bootstrap.php"
    stopOnError="false"
    stopOnFailure="false"
    stopOnIncomplete="false"
    stopOnSkipped="false"
    cacheResult="false"
    printerClass="Codedungeon\PHPUnitPrettyResultPrinter\Printer"
>
  <coverage>
    <include>
      <directory>src</directory>
    </include>
  </coverage>
  <php>
    <ini name="error_reporting" value="-1"/>
    <server name="KERNEL_CLASS" value="\App\Kernel" />
    <server name="APP_ENV" value="test" force="true"/>
    <server name="SHELL_VERBOSITY" value="-1"/>
    <server name="SYMFONY_PHPUNIT_REMOVE" value=""/>
    <env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
    <env name="CORS_ALLOW_ORIGIN" value="^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$"/>
  </php>
  <extensions>
    <extension class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension"/>
  </extensions>
  <testsuites>
    <testsuite name="Project Test Suite">
      <directory>tests</directory>
    </testsuite>
  </testsuites>
</phpunit>

【问题讨论】:

    标签: phpunit php-code-coverage


    【解决方案1】:

    你指的是phpunit text-output for coverage

      --coverage-text=<file>      Generate code coverage report in text format [default: standard output]
    

    它的替代方法是一个小脚本,您可以在运行覆盖率的测试后运行它以检查收集的数据。

    这里是composer.json{"script": {}}部分的摘录:

        "unit-test": [
          "@phpunit --log-junit build/log/junit.xml --coverage-clover build/log/clover.xml test/unit",
          "@php -f lib/build/coverage-checker.php -- build/log/clover.xml"
        ],
    

    取自具有脚本的现有项目:

    这是源自这篇博文的维护版本:

    默认检查的百分比是 100%。如果您想降低它,可以将它作为第二个位置参数传递。

    【讨论】:

    • 感谢您提供出色的公式化答案,我会尽快提供反馈。这看起来很有希望!
    • 我不得不更改一行以使其在我的环境中运行:第 95 行 printf(' %6.2f: %s%s', $coverage, $file-&gt;attributes()['name'], PHP_EOL);
    猜你喜欢
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 2012-01-18
    • 2012-01-14
    • 2014-06-27
    • 2012-08-04
    相关资源
    最近更新 更多