Phpunit 覆盖范围:“addUncoveredFilesFromWhitelist"和“processUncoveredFilesFromWhitelist"选项有什么区别?

2022-01-25 00:00:00 php phpunit

我正在尝试为特定目录的 phpunit 设置代码覆盖率.谁能告诉我有什么区别:

I'm trying to set up code coverage for phpunit for a particular directory. Can someone tell me what is the difference between:

<filter>
    <whitelist>
        <directory suffix=".php">lib/</directory>
    </whitelist>
</filter>

<filter>
    <whitelist addUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">lib/</directory>
    </whitelist>
</filter>

<filter>
    <whitelist processUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">lib/</directory>
    </whitelist>
</filter>

目前前 2 个选项将起作用(具有不同的覆盖率),但第三个选项将失败并出现类似于 如何将未发现的文件添加到 Yii 应用程序的 PHPUnit 代码覆盖率报告中.

Currently the first 2 options will work (with different coverage numbers) but the third one will fail with errors similar to How to add uncovered files to PHPUnit code coverage report of the Yii application.

刚开始使用 phpunit,想了解这些白名单选项之间的区别.我阅读了有关此的官方文档,但我不确定我是否理解.

Just starting out with phpunit and would like to understand the differences between these whitelisting options. I read the official docs on this but I'm not sure I understand.

推荐答案

快速浏览php-code-coverage GitHub上的包揭示了真相:

A quick peek in the source code of php-code-coverage package on GitHub reveals the truth:

  • 如果 addUncoveredFilesFromWhitelistFALSE 则代码覆盖率包含有关已加载和执行的文件的信息(仅包含包含代码的行);
    在这种情况下,processUncoveredFilesFromWhitelist 的值会被忽略;
  • 如果 addUncoveredFilesFromWhitelistTRUE 那么白名单中未加载和执行的文件也将包含在代码覆盖范围内:
    • 如果 processUncoveredFilesFromWhitelistFALSE 则不以任何方式处理文件;它们的所有行都将出现在代码覆盖范围内,即使是空行和仅包含注释的行也是如此;这是完成工作的快速而肮脏的方式;
    • 如果 processUncoveredFilesFromWhitelistTRUE 然后包含文件并使用 XDebug 的代码覆盖功能(与实际运行的文件相同)仅将包含代码的行放入报告中;这是缓慢而努力的方式.
    • if addUncoveredFilesFromWhitelist is FALSE then the code coverage contains information about the files that were loaded and executed (only the lines that contain code are included);
      the value of processUncoveredFilesFromWhitelist is ignored in this case;
    • if addUncoveredFilesFromWhitelist is TRUE then the files from the white list that were not loaded and executed will be included in the code coverage too:
      • if processUncoveredFilesFromWhitelist is FALSE then the files are not processed in any way; all their lines will appear in the code coverage as not being executed, even the empty lines and the lines that contain only comments; this is the quick and dirty way to get the things done;
      • if processUncoveredFilesFromWhitelist is TRUE then the files are included and XDebug's code coverage functionality is used (the same as for the files that actually ran) to put into the report only the lines that contain code; this is the slow hard-working way.

      addUncoveredFilesFromWhitelist 的默认值为 TRUEprocessUncoveredFilesFromWhitelist 的默认值为 FALSE.这意味着白名单中未被覆盖的文件(因为它们没有运行)使用快速方式包含在报告中,并且它们的覆盖率报告虽然准确 (0%) 是使用计算的总行数略大于实际行数.

      The default value for addUncoveredFilesFromWhitelist is TRUE and for processUncoveredFilesFromWhitelist is FALSE. This means the files from the whitelist that were not covered (because they didn't run) are included in the report using the quick way and their coverage report, while exact (0%), is computed using a total number of rows slightly bigger than the real one.

      但是,由于 0 仍然是 0%,它认为这是在报告中包含未发现文件的最佳方式.

      However, since 0 out of anything is still 0%, it think this is the best way to include the uncovered files in the report.

相关文章