alexyav / phpspec-code-coverage

为 PhpSpec 测试生成代码覆盖率报告

v4.3.3 2020-06-25 09:38 UTC

README

Latest Stable Version Total Downloads Scrutinizer Code Quality Travis

phpspec-code-coverage 是一个 PhpSpec 扩展,用于为 PhpSpec 测试生成代码覆盖率报告。

生成代码覆盖率报告允许您分析代码库中哪些部分被测试以及测试效果如何。然而,仅凭代码覆盖率不应作为衡量测试质量的唯一指标。

phpspec-code-coverage console report phpspec-code-coverage HTML report

要求

变更日志

请参阅 CHANGELOG.md 了解最近更改的信息。

安装

将此包作为项目中的开发依赖项安装

$ composer require --dev friends-of-phpspec/phpspec-code-coverage

通过编辑项目中的 phpspec.yml 启用扩展

extensions:
  FriendsOfPhpSpec\PhpSpec\CodeCoverage\CodeCoverageExtension: ~

这足以通过使用扩展提供的默认值来启用代码覆盖率生成。此扩展支持各种 配置选项。要查看完全注释的示例配置文件,请参阅 配置部分

用法

如果执行 phpspec run 命令,您将在 coverage 目录中看到生成的代码覆盖率(以 html 格式)

$ bin/phpspec run

注意! 生成代码覆盖率报告时,请确保 PHP 进程通过 CLI 运行时不受内存限制(即在 /etc/php/cli/php.ini 中将 memory_limit 设置为 -1)。

使用 phpdbg 运行

此扩展现在支持 phpdbg,使用较新版本的 PHP 时可加快执行速度。通过 phpdbg 运行 phpspec

$ phpdbg -qrr phpspec run

注意! PHP 7+ 需要 phpdbg 生成代码。

配置

下面是一个完全注释的 phpspec.yml 示例文件,您可以使用它作为进一步自定义扩展默认设置的起点。以下配置文件包含所有 配置选项

# phpspec.yml
# ...
extensions:
  # ... other extensions ...
  # friends-of-phpspec/phpspec-code-coverage
  FriendsOfPhpSpec\PhpSpec\CodeCoverage\CodeCoverageExtension:
    # Specify a list of formats in which code coverage report should be
    # generated.
    # Default: [html]
    format:
      - text
      - html
      #- clover
      #- php
      #- xml
    #
    # Specify output file/directory where code coverage report will be
    # generated. You can configure different output file/directory per
    # enabled format.
    # Default: coverage
    output:
      html: coverage
      #clover: coverage.xml
      #php: coverage.php
      #xml: coverage
    #
    # Should uncovered files be included in the reports?
    # Default: true
    #show_uncovered_files: true
    #
    # Set lower upper bound for code coverage
    # Default: 35
    #lower_upper_bound: 35
    #
    # Set high lower bound for code coverage
    # Default: 70
    #high_lower_bound: 70
    #
    # Whilelist directories for which code generation should be done
    # Default: [src, lib]
    #
    whitelist:
      - src
      - lib
    #
    # Whiltelist files for which code generation should be done
    # Default: empty
    #whilelist_files:
      #- app/bootstrap.php
      #- web/index.php
    #
    # Blacklist directories for which code generation should NOT be done
    #blacklist:
      #- src/legacy
    #
    # Blacklist files for which code generation should NOT be done
    #blacklist_files:
      #- lib/bootstrap.php

选项

  • format(可选):代码覆盖率应生成的格式列表。可以是以下之一: cloverphptexthtmlxml(默认 html注意:当使用 clover 格式选项时,您必须配置特定于 clover 格式的 output 文件(见下文)。
  • output(可选):设置特定代码覆盖率格式将生成的输出文件/目录。如果您配置了多个格式,则采用 format:output 的哈希(例如 clover:coverage.xml)(默认 coverage
  • show_uncovered_files(可选):包括未覆盖的文件在覆盖率报告中(默认 true
  • lower_upper_bound(可选):设置代码覆盖率的上下限(默认 35)。
  • high_lower_bound(可选)设置代码覆盖率的高下限(默认为70
  • whitelist接受一个白名单目录数组(默认:libsrc)。
  • whitelist_files接受一个白名单文件数组(默认:无)。
  • blacklist接受一个黑名单目录数组(默认:testvendorspec
  • blacklist_files接受一个黑名单文件数组

作者

版权所有(c)2017-2018 ek9 dev@ek9.co(《https://ek9.co》)。

版权所有(c)2013-2016 Henrik Bjornskov,部分代码来自henrikbjorn/phpspec-code-coverage项目。

许可协议

遵循MIT许可证