是否可以从 PHPUnit 的代码覆盖范围中强制排除文件夹?
我遇到的问题是,我有一个 Symfony 1.4 项目,其中的文件夹位于
./lib/vendor/symfony/*
。我想递归地排除 ./lib/vendor/*
中的任何内容。
现在,我想排除它们,无论它们是否被我的测试隐式覆盖,即我永远不想看到这些文件夹。因此,我已将这一点添加到我的
phpunit.xml
配置文件中,但无论我做什么,它似乎都没有排除这些文件夹:
<filter>
<whitelist>
<exclude>
<directory>./lib/vendor/*</directory>
<directory>./lib/vendor/symfony/lib/*</directory>
</exclude>
</whitelist>
</filter>
在我看来,一旦代码被命中并且 XDebug 注意到它,PHPUnit 无论如何都会将其包含在代码覆盖范围中。对我来说,这样做的缺点是,这段代码已经由 Symfony 开发人员测试过,所以不需要将其包含在我的覆盖率报告中,从而弄乱我的数字:P
在 PHPUnit 的最新版本中从覆盖率报告中排除某些文件的正确方法是使用
<exclude>
指令而不是 <blacklist>
。例如:
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
<exclude>
<directory suffix=".php">src/Legacy/</directory>
<file>src/example.php</file>
</exclude>
</whitelist>
</filter>
对于使用最新模式的最新 PHPUnit,这将是:
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
<exclude>
<directory suffix=".php">src/Legacy/</directory>
<file>src/example.php</file>
</exclude>
</coverage>
好吧,所以我认为您可以拥有黑名单部分或白名单部分,事实证明您可以同时拥有两者,所以我将这些文件夹列入黑名单并且它起作用了:
<filter>
<blacklist>
<directory>./lib/vendor</directory>
<directory>./lib/helper</directory>
</blacklist>
</filter>
观察:与黑名单相比,白名单(您希望包含在代码覆盖率中的目录)使整个测试套件的 phpunit 执行速度更快。
<filter>
<whitelist>
<directory>/my/project/directory/to/be/covered</directory>
....
</whitelist>
</filter>
测试使用:PHPUnit 4.5.0 和 PHP 5.5.9
这些答案似乎都适用于旧版本的
PHPUnit
我正在运行
PHPUnit 5.7.23
,但在包含和排除 phpunit
的文件时遇到问题。 syntax 似乎已经发生了很大的变化,并且仅部分向后兼容。我有一个复杂的要求,我还需要包含和排除单独的目录以实现代码覆盖(它是一个遗留系统)。
这是我所需要的
phpunit.xml
:
<testsuites>
<testsuite name="Unit Tests">
<directory suffix="Test.php">./tests</directory>
<exclude>./tests/blah/excluded_file_from_tests1.php</exclude>
<exclude>./tests/blah/excluded_file_from_tests2.php</exclude>
<exclude>./tests/blah/excluded_file_from_tests3.php</exclude>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./common/lib/included_directory</directory>
<exclude>
<directory suffix=".php">./common/lib/included_directory/core/blah/excluded_directory</directory>
</exclude>
</whitelist>
</filter>
所以:
<testsuites>
下我包括./tests
目录.tests
下我想排除一组文件excluded_file_from_testsX.php
./common/lib/included_directory
因此,通过
phpunit --coverage-html build/coverage
运行时,这似乎效果很好
适用于 PHPUnit 9.0.1
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">DIRECTORY_TO_ADD</directory>
<directory suffix=".php">ANOTHER_DIR_TO_ADD</directory>
<exclude>
<file>ANOTHER_DIR_TO_ADD/file1_to_not_include.php</file>
<file>ANOTHER_DIR_TO_ADD/file2_to_not_include.php</file>
</exclude>
</whitelist>
</filter>
从 PHPUnit 10.x 开始,随着删除
这里是一个与使用新元素结构的 OP 相关的主要工作的 phpunit.xml 示例。将 块添加到现有
<phpunit ...>
<source>
<include>
<directory suffix=".php">app</directory>
<directory suffix=".php">lib</directory>
<!-- Add any other project directories you want included by PHPUnit here -->
</include>
<exclude>
<directory suffix=".php">lib/vendor</directory>
<!-- Filter down additional unwanted subdirectories here -->
</exclude>
</source>
</phpunit>