• 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 在尾部加如下 
    1. [Xdebug]
    2. zend_extension="D:\php\php_xdebug-3.3.0alpha3-8.2-vs16-x86_64.dll"
    3. xdebug.mode=coverage

    其中zend_extension是Xdebug的绝对路径

    配置

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

    1. "1.0" encoding="UTF-8"?>
    2. <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php"
    3. xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
    4. cacheDirectory="testReports/.phpunit.cache">
    5. <source>
    6. <include>
    7. <directory suffix=".php">srcdirectory>
    8. include>
    9. source>
    10. <coverage>
    11. <report>
    12. <cobertura outputFile="testReports/cobertura.xml" />
    13. report>
    14. coverage>
    15. <logging>
    16. <junit outputFile="testReports/junit.xml" />
    17. logging>
    18. phpunit>

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

    phpunit --migrate-configuration

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

    1. <report>
    2. <clover outputFile="clover.xml"/>
    3. <cobertura outputFile="cobertura.xml"/>
    4. <crap4j outputFile="crap4j.xml" threshold="50"/>
    5. <html outputDirectory="html-coverage" lowUpperBound="50" highLowerBound="90"/>
    6. <php outputFile="coverage.php"/>
    7. <text outputFile="coverage.txt" showUncoveredFiles="false" showOnlySummary="true"/>
    8. <xml outputDirectory="xml-coverage"/>
    9. report>

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

    测试结果所有选项如下

    1. <logging>
    2. <junit outputFile="junit.xml"/>
    3. <teamcity outputFile="teamcity.txt"/>
    4. <testdoxHtml outputFile="testdox.html"/>
    5. <testdoxText outputFile="testdox.txt"/>
    6. logging>

    Azure DevOps Pipelines的配置

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

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

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

    发布测试结果

    1. - task: PublishTestResults@2
    2. inputs:
    3. testRunner: "JUnit"
    4. # Make sure the file name matches the file name you specified.
    5. testResultsFiles: "**/testReports/junit.xml"
    6. failTaskOnFailedTests: true
    7. displayName: "Publish Test Results"

    发布测试覆盖率

    1. - task: PublishCodeCoverageResults@1
    2. inputs:
    3. codeCoverageTool: Cobertura
    4. summaryFileLocation: '**/testReports/cobertura.xml'

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

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

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

  • 相关阅读:
    APP安全加固怎么做?加固技术、加固方法、加固方案
    nodejs+vue校园失物招领平台
    Go语言之return语句深入理解及defer、return的执行顺序
    串口通信的基本原理
    hive静态分区和动态分区
    pkg-config使用
    生产部长修炼宝典③:解放统计员、释放生产力—基于数据建模平台的绩效管理
    javax.net.ssl.SSLException: Connection reset
    SQL必需掌握的100个重要知识点:组合 WHERE 子句
    刷题记录:牛客NC51180Accumulation Degree
  • 原文地址:https://blog.csdn.net/zhangmike/article/details/134319630