如何生成代码覆盖率并排除第三方库?
解读
在国内 WordPress 项目面试中,面试官真正想确认的是:
- 你是否能把 PHPUnit + PCOV/Xdebug 这套官方推荐方案落地到 CI(GitHub Actions / Gitee Go / Jenkins);
- 能否用 phpunit.xml 精准 exclude vendor/、node_modules/、tests/fixtures/ 等第三方目录,避免覆盖率虚高;
- 是否理解 WordPress 特殊常量(WP_TESTS_PHPUNIT_POLYFILLS_PATH、WP_PLUGIN_DIR) 对覆盖率路径的影响;
- 最终能否把 HTML + Clover XML 双格式报告推到 SonarQube 或 Codecov,让团队看得见、可回溯。
回答时务必给出 可复制的命令行 + 配置文件片段,并解释 PCOV 与 Xdebug 在性能上的 5~8 倍差距,这是国内一线团队最关心的落地细节。
知识点
- PHPUnit >= 9.5 的
<coverage><include><exclude>语法 - PCOV 扩展(PHP 7.1+)与 Xdebug3 mode=coverage 的切换方式
- Composer 脚本钩子:
composer test-coverage统一入口 - WordPress 测试套件 bootstrap 对
ABSPATH的重定义导致路径漂移的修正方法 - CI 缓存策略:
.phpunit.cache/code-coverage目录加速二次运行 - SonarQube 中文社区版对
clover.xml的解析规则(必须 relativePath 以./开头)
答案
-
本地一次性验证
# 安装 PCOV(Ubuntu 20.04 示例) sudo apt install php8.1-pcov # 确认扩展生效 php -m | grep pcov # 安装依赖 composer install --no-dev --optimize-autoloader composer install --dev # 生成覆盖率 composer test-coverage其中
composer.json脚本段:"scripts": { "test": "phpunit", "test-coverage": "phpunit --coverage-html coverage/html --coverage-clover coverage/clover.xml" } -
核心配置文件
phpunit.xml.dist(放在插件/主题根目录,随包发布)<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/bootstrap.php" cacheResultFile=".phpunit.cache/test-results" executionOrder="depends,defects" beStrictAboutOutputDuringTests="true" beStrictAboutTodoAnnotatedTests="true" failOnRisky="true" failOnWarning="true" verbose="true"> <coverage cacheDirectory=".phpunit.cache/code-coverage"> <include> <directory suffix=".php">inc</directory> <directory suffix=".php">src</directory> </include> <exclude> <directory>vendor</directory> <directory>tests</directory> <directory>node_modules</directory> <file>inc/legacy-php52.php</file> </exclude> <report> <html outputDirectory="coverage/html"/> <clover outputFile="coverage/clover.xml"/> </report> </coverage> </phpunit>要点:
<exclude>节点必须写在<include>之后,否则 PHPUnit 9 会报错;cacheDirectory把覆盖率中间文件缓存到.phpunit.cache/code-coverage,CI 第二次运行可省 30~50 s;- legacy-php52.php 这类兼容文件直接按文件排除,避免拉低整体覆盖率。
-
WordPress 测试启动文件
tests/bootstrap.php必须修正路径漂移$_tests_dir = getenv( 'WP_TESTS_DIR' ) ?: sys_get_temp_dir() . '/wordpress-tests-lib'; require_once $_tests_dir . '/includes/functions.php'; // 让 PHPUnit 识别插件根目录为代码根 define( 'WP_PLUGIN_DIR', dirname( __DIR__ ) ); tests_add_filter( 'muplugins_loaded', function () { require dirname( __DIR__ ) . '/plugin.php'; } ); require $_tests_dir . '/includes/bootstrap.php';若缺失这一步,PHPUnit 会把
/tmp/wordpress/src当成主目录,导致覆盖率 0%。 -
CI 集成(以 Gitee Go 为例,国内镜像快)
kind: pipeline name: 覆盖率检查 steps: - name: install image: khs1994/php:8.1-composer commands: - composer install --dev --no-interaction - name: test image: khs1994/php:8.1-pcov commands: - composer test-coverage artifacts: - coverage/clover.xml - name: sonar image: sonarsource/sonar-scanner-cli:4.8 settings: sonar_token: ${SONAR_TOKEN}在 SonarQube 项目设置里把
sonar.php.coverage.reportPaths=coverage/clover.xml写死,中文界面即可识别。 -
结果验证
运行完毕后打开coverage/html/index.html,红线部分即为被排除的第三方库;在 CLI 执行grep -E '^\s*<file name="vendor/' coverage/clover.xml若返回空,说明排除成功。
拓展思考
- Monorepo 场景:多个插件共用同一
vendor/时,可把phpunit.xml拆成phpunit.plugin-a.xml、phpunit.plugin-b.xml,各自<include>对应src/PluginA目录,避免交叉统计。 - Mutation Testing:用 Infection 在覆盖率大于 80% 的基线上做变异测试,国内金融客户已把它写进验收标准;Infection 会自动读取 PHPUnit 生成的
coverage-xml目录,无需重复跑用例。 - 性能调优:PCOV 在 PHP 8.2 下对 10 万行代码跑全量覆盖率仅需 6 s,而 Xdebug3 需 45 s;阿里云 2 vCPU 的流水线小时计费,单次可省 0.03 元,一年累计 1 万次的项目能省 300 元,老板看得见成本收益。