【问题标题】:Publish PHPUnit code coverage result in Pipeline Azure在 Pipeline Azure 中发布 PHPUnit 代码覆盖率结果
【发布时间】:2023-03-23 07:41:01
【问题描述】:

Azure DevOps Pipelines 仅支持 JaCoCo 和 Cobertura 覆盖率报告格式:

PHPUnit 仅支持 Clover、Crap4jn PHP、(自定义)XML、HTML 和 TXT 覆盖率报告格式:

如何在我的 Pipeline 中发布我的 PHPUnit 测试的覆盖率结果?

【问题讨论】:

    标签: azure azure-devops phpunit code-coverage


    【解决方案1】:

    不过,目前不支持在 Pipeline 中发布 PHPUnit 代码覆盖率结果。

    【讨论】:

      【解决方案2】:

      PHPUnit 9.4 添加了对 Cobertura 覆盖输出的支持。但是,Azure Pipelines 提供 atm 的默认 Ubuntu 构建代理。仅支持 phpunit 8.5。但是您可以通过在 docker 容器中运行 phpunit 9.4+ 来获取覆盖率报告。这是我当前的 azure 构建管道的 sn-p :

      trigger:
      - master
      
      pool:
        vmImage: ubuntu-latest
      
      variables:
        phpVersion: 7.4
        phpunitImage: jitesoft/phpunit:7.4-9
      
      steps:
      - 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)
          php -version
        displayName: 'Use PHP version $(phpVersion)'
      
      # Do a composer install to get an autoloader that phpunit can use
      - script: composer install --no-interaction --prefer-dist
        displayName: 'composer install'
      
      # Run the test using the jitesoft phpunit docker image to get support
      # for phpunit 9+ and that way cobertura reports for code coverage.
      - script: |
          docker run --rm -v $(pwd):/app ${{ variables.phpunitImage }} phpunit --log-junit .junit/TEST-phpunit-junit.xml --coverage-cobertura=.coverage/COVERAGE-phpunit-cobertura.xml
        displayName: 'Run tests with phpunit docker container'
      
      - task: PublishTestResults@2
        displayName: 'Publish test report'
        condition: always()
        inputs:
          testResultsFormat: 'JUnit'
          testResultsFiles: '**/TEST-phpunit-*.xml'
          searchFolder: '$(System.DefaultWorkingDirectory)/.junit'
          failTaskOnFailedTests: true
      
      - task: PublishCodeCoverageResults@1
        displayName: 'Publish coverage report'
        condition: always()
        inputs:
          codeCoverageTool: 'Cobertura'
          summaryFileLocation: '$(System.DefaultWorkingDirectory)/.coverage/COVERAGE-phpunit-*.xml'
          pathToSources: '$(System.DefaultWorkingDirectory)/src'
          failIfCoverageEmpty: true
      

      注意 Publish* 任务中的 always() 条件。这是必需的,因为如果测试失败,那么docker run 步骤将失败并显示 bash 退出代码 1,这反过来会阻止报告发布,除非这些步骤是强制的。可能有一种方法可以处理退出代码清理器,但我还没有弄清楚。

      ps。理想情况下,你也应该做一些缓存,这样 docker 镜像就不会总是被下载,但我跳过了这部分,让示例专注于单元测试和覆盖率报告的实际运行。

      【讨论】:

        猜你喜欢
        • 2021-05-25
        • 2011-01-18
        • 2012-05-04
        • 1970-01-01
        • 2022-08-09
        • 2021-08-13
        • 2022-01-12
        • 1970-01-01
        • 2021-01-17
        相关资源
        最近更新 更多