【问题标题】:PHP Unit Testing CoveragePHP 单元测试覆盖率
【发布时间】:2014-10-15 15:42:17
【问题描述】:

我是 PHP 开发新手,但有开发 Python Web 应用程序的经验。在 Python 中,有一个名为 Coverage 的包可以分析代码并识别缺少单元测试的功能。

PHP 世界中是否存在这样的包?我已经搜索了 Google 和 SO,但没有找到。感谢您的帮助!

【问题讨论】:

    标签: php code-coverage


    【解决方案1】:

    PHPUnit 内置了覆盖率。您可以使用生成 html 覆盖率报告

    phpunit --coverage-html /[path where to save report]
    

    另一个选项是--coverage-clover,而不是--coverage-html。这将生成一份关于所涵盖内容的 xml 报告。

    如果您使用像 phpStorm 这样的高级 IDE,您只需右键单击测试并选择“Run with coverage”,它将在编辑器的文件资源管理器中显示覆盖率。

    【讨论】:

    • 对于终端只需运行phpunit --coverage-text
    【解决方案2】:

    PHPUnit 支持代码覆盖并且是事实上的标准。与 Jenkins 等人集成。

    https://phpunit.de/manual/current/en/code-coverage-analysis.html

    【讨论】:

      【解决方案3】:

      是的,有很多代码覆盖工具。 将以下链接添加到您的 phpunit.xml

      <logging>
          <log type="coverage-html" target="./mainreport" charset="UTF-8"
               yui="true" highlight="true"
               lowUpperBound="50" highLowerBound="80" />
      </logging>
      

      安装 XDEBUG , (ex: for ubuntu and php7 - sudo apt-get install php7.0-xdebug)

      这会将您的报告记录到目标属性中指定的目录 (target="./mainreport")。报告也将采用 html 格式

      在您的根目录中创建一个mainreport 目录。 运行单元测试 在浏览器中打开 index.html 可以看到覆盖率报告。

      【讨论】:

      • 为我工作
      【解决方案4】:

      PHPUnit 本身有使用PHP_CodeCoverage的覆盖工具

      此页面显示所有不同的覆盖选项:https://phpunit.de/manual/current/en/textui.html

      一个示例 html 输出覆盖命令行将是:

      phpunit ./report tests/*
      

      这将创建一个名为 report 的文件夹,并包含 tests/ 文件夹下所有测试的所有覆盖率

      【讨论】:

        【解决方案5】:

        您还可以使用 json 文件检查项目模块 Wise 中有多少案例通过或失败,从而生成案例报告。

        将此代码添加到您的 phpnit.xml 文件中:

        <log type="json" target="./log/jsonreport.json" charset="UTF-8"/>
        

        运行后,生成的已通过案例列表文件(jsonreport.json)应该存在。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-18
          • 1970-01-01
          • 1970-01-01
          • 2016-04-07
          • 2023-04-07
          • 2014-05-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多