2023年11月PHP测试覆盖率解决方案

【题记:最近进行了ExcelBDD PHP版的开发,查阅了大量资料,发现PHP测试覆盖率解决方案存在不同的历史版本,让我花费了蛮多时间,为了避免后人浪费时间,整理本文,而且网上没有给出Azure DevOps里面PHP测试覆盖率的解决方案,本文一并给出】

版本

  • PHP 8.2.12

  • PHPUnit 10.4.2

  • php-code-coverage 10.1.7

  • Xdebug v3.3.0alpha3

  • 操作系统:Win11

安装

  1. 分别安装PHP, Composer(见 Composer

  2. 安装PHPUnit,php-code-coverage

  3. 根据网上指南配置PHPUnit,比如PHPUnit 手册 -- 第 1 章 安装 PHPUnit

  4. 下载Xdebug,见Xdebug: Downloads

  5. 配置php.ini 在尾部加如下

    [Xdebug]
    zend_extension="D:\php\php_xdebug-3.3.0alpha3-8.2-vs16-x86_64.dll"
    xdebug.mode=coverage

其中zend_extension是Xdebug的绝对路径

配置

PHPUnit前后版本在phpunit.xml上历经了多次修改,网上能够查到的都是旧版,浪费了我好多时间。如下是最新格式。

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php"
    xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
    cacheDirectory="testReports/.phpunit.cache">
    <source>
        <include>
            <directory suffix=".php">src</directory>
        </include>
    </source>
    <coverage>
        <report>
            <cobertura outputFile="testReports/cobertura.xml" />
        </report>
    </coverage>

    <logging>
        <junit outputFile="testReports/junit.xml" />
    </logging>

</phpunit>

网上容易查到的"filter","whitelist"已经过时。PHPUnit作者考虑到前后变化,提供了自动改写功能,如下:

bash 复制代码
phpunit --migrate-configuration

目前PHPUnit支持的覆盖率格式如下

XML 复制代码
<report>
    <clover outputFile="clover.xml"/>
    <cobertura outputFile="cobertura.xml"/>
    <crap4j outputFile="crap4j.xml" threshold="50"/>
    <html outputDirectory="html-coverage" lowUpperBound="50" highLowerBound="90"/>
    <php outputFile="coverage.php"/>
    <text outputFile="coverage.txt" showUncoveredFiles="false" showOnlySummary="true"/>
    <xml outputDirectory="xml-coverage"/>
</report>

参见 5. The XML Configuration File --- PHPUnit 10.4 Manual

测试结果所有选项如下

XML 复制代码
<logging>
    <junit outputFile="junit.xml"/>
    <teamcity outputFile="teamcity.txt"/>
    <testdoxHtml outputFile="testdox.html"/>
    <testdoxText outputFile="testdox.txt"/>
</logging>

Azure DevOps Pipelines的配置

微软官方没有给出PHP测试覆盖率解决方案,经过摸索,得到如下方案:

在azure-pipelines.yml对应位置,按下修改来准备好环境

bash 复制代码
- script: |
    sudo update-alternatives --set php /usr/bin/php$(phpVersion)
    sudo update-alternatives --set phar /usr/bin/phar$(phpVersion)
    sudo update-alternatives --set phpdbg /usr/bin/phpdbg$(phpVersion)
    sudo update-alternatives --set php-cgi /usr/bin/php-cgi$(phpVersion)
    sudo update-alternatives --set phar.phar /usr/bin/phar.phar$(phpVersion)
# 如下这行没有必要,azure的VM已经安装了xdebug
    sudo apt-get install php8.1-xdebug
# 下行必需,php.ini的位置可以通过 php -i 得到
    sudo sh -c "echo 'xdebug.mode=coverage' >> /etc/php/8.1/cli/php.ini"
    php -version
  displayName: 'Use PHP version $(phpVersion)'

发布测试结果

bash 复制代码
- task: PublishTestResults@2
  inputs:
    testRunner: "JUnit"
    # Make sure the file name matches the file name you specified.
    testResultsFiles: "**/testReports/junit.xml"
    failTaskOnFailedTests: true
  displayName: "Publish Test Results"

发布测试覆盖率

bash 复制代码
- task: PublishCodeCoverageResults@1
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '**/testReports/cobertura.xml'

提示:PublishCodeCoverageResults@2达到的效果不如PublishCodeCoverageResults@1。

ExcelBDD PHP版在Azure DevOps配置了CI,见ExcelBDD PHP CI

全部文件见ExcelBDD开源项目PHP部分

相关推荐
m0_547486668 小时前
《JavaScript核心原理 》全套PPT课件2026
开发语言·javascript·ecmascript
MC皮蛋侠客8 小时前
Tauri 2.x 系列(五):调用系统 API——官方插件、Rust crate 与原生能力
开发语言·后端·rust
布莱克6059 小时前
软中断和硬中断的区别
开发语言·操作系统
whcyhhh10 小时前
头歌实践教学平台:数据科学与大数据技术导论(十二)
大数据·开发语言·python·数据清洗
lzhdim10 小时前
13、JavaScript事件循环机制 - JavaScript学习系列文章
开发语言·前端·javascript·学习·ecmascript
zbyyd11 小时前
Linux 多线程:互斥锁 &amp; 信号量
linux·c语言·开发语言·网络
wuyk55511 小时前
从零吃透Modbus通信|第3章:STM32 Modbus RTU主机
服务器·开发语言·网络·数据结构·stm32·单片机·嵌入式硬件
one31212 小时前
高精度MEMS电容式倾角传感器:原理、结构与Python三维可视化
开发语言·python
程序员小八77712 小时前
Go 语言特性
开发语言·后端·golang
MartinYeung513 小时前
[论文学习]小型代码语言模型中的潜伏代理行为研究
学习·语言模型·php