Phpunit 覆盖率:'addUncoveredFilesFromWhitelist' 和 'processUncoveredFilesFromWhitelist' 选项有什么区别?

Phpunit coverage: what is the difference between 'addUncoveredFilesFromWhitelist' and 'processUncoveredFilesFromWhitelist' options?

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

<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>

目前前两个选项可以使用(覆盖率不同),但第三个选项将失败并出现类似于 How to add uncovered files to PHPUnit code coverage report of the Yii application.

的错误

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

所以我想我错了。以下是 documentation 对此的评价:

Optionally, all whitelisted files can be added to the code coverage report by setting addUncoveredFilesFromWhitelist="true" in your PHPUnit configuration (see the section called “Including and Excluding Files for Code Coverage”). This allows the inclusion of files that are not tested yet at all. If you want to get information about which lines of such an uncovered file are executable, for instance, you also need to set processUncoveredFilesFromWhitelist="true" in your PHPUnit configuration (see the section called “Including and Excluding Files for Code Coverage”).

所以这里的答案是 adding 未发现的文件会将它们包含在覆盖率报告中,但实际上 processing 它们会收集更多信息。

原创

参见塞巴斯蒂安的 this twitter conversation。很难从 Twitter 的简洁性中分辨出来,但看起来 addUncoveredFilesFromWhitelist 可能只是 processUncoveredFilesFromWhitelist 为遗留代码提供的相同功能的旧形式。

推特对话内容为:

@user1: In @phpunit, what’s the difference between addUncoveredFilesFromWhitelist="true" and processUncoveredFilesFromWhitelist="true" ?

@s_bergmann: You want to use processUncoveredFilesFromWhitelist. If it does not work (legacy code) then use addUncoveredFilesFromWhitelist

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

  • 如果 addUncoveredFilesFromWhitelistFALSE 则代码覆盖率包含有关已加载和执行的文件的信息(仅包括包含代码的行);
    processUncoveredFilesFromWhitelist 的值在这种情况下被忽略;
  • 如果 addUncoveredFilesFromWhitelistTRUE 那么白名单中未加载和执行的文件也将包含在代码覆盖范围内:
    • 如果 processUncoveredFilesFromWhitelistFALSE 那么文件不会以任何方式处理;它们的所有行都将在代码覆盖率中显示为未执行,即使是空行和仅包含注释的行也是如此;这是完成任务的快速而肮脏的方法;
    • 如果 processUncoveredFilesFromWhitelistTRUE 则包含文件并使用 XDebug 的代码覆盖功能(与实际 运行 的文件相同)仅放入报告中包含代码的行;这就是慢吞吞的方法。

addUncoveredFilesFromWhitelist 的默认值为 TRUEprocessUncoveredFilesFromWhitelist 的默认值为 FALSE。这意味着白名单中未被覆盖的文件(因为它们没有 运行)将使用快速方法包含在报告中,并且它们的覆盖率报告是使用以下方法计算的(0%)总行数比实际行数略多。

但是,由于 0 仍然是 0%,它认为这是将未覆盖文件包含在报告中的最佳方式。