无法在 Azure 管道中发布代码覆盖率

Unable to publish code coverage in azure pipelines

当 运行 Pytest 通过 Azure 管道时管道无法发布代码覆盖率结果,然后在管道的输出中您可以看到文件已生成。在生成覆盖率报告之前,PublishCodeCoverageResults@1 任务似乎是 运行。 test.yml 文件如下所示:

      - script: |
          python -m pytest src/ -v -o junit_family=xunit2 --junitxml=junit/test-results.xml --cov=src/ --cov-report=xml
        displayName: Run tests
      - task: PublishTestResults@2
        condition: succeededOrFailed()
        inputs:
          testResultsFiles: 'junit/**.xml'
          testRunTitle: 'Publish test results for ${{ parameters.projectName }}'
      - task: PublishCodeCoverageResults@1
        inputs:
          codeCoverageTool: Cobertura
          summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml'

这是管道输出的屏幕截图:

错误行如下:

##[error]Unable to process command '##vso[codecoverage.publish codecoveragetool=Cobertura;summaryfile=/agent/_work/116/s/coverage.xml;reportdirectory=/agent/_work/116/s/htmlcov;]' successfully. Please reference documentation (http://go.microsoft.com/fwlink/?LinkId=817296)

如有任何帮助,我们将不胜感激

  • 您需要生成 cobertura 覆盖率报告,并且需要将其添加到配置中。
  • coverage.xml替换为下面的cobertura-coverage.xml
summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/cobertura-coverage.xml'
  • 我修改了你的代码
- script: |
         python -m pytest src/ -v -o junit_family=xunit2 --junitxml=junit/test-results.xml --cov=src/ --cov-report=xml
       displayName: Run tests
     - task: PublishTestResults@2
       condition: succeededOrFailed()
       inputs:
         testRunner: JUnit
         testResultsFiles: 'junit/**.xml'
         testRunTitle: 'Publish test results for ${{ parameters.projectName }}'
     - task: PublishCodeCoverageResults@1
       inputs:
         codeCoverageTool: Cobertura
         summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/cobertura-coverage.xml'

参考资料

  • Publish code coverage not finding coverage file in Azure DevOps

问题是由于 pytest-azurepipelines 库造成的,如下所示: https://github.com/tonybaloney/pytest-azurepipelines/issues/33

将 --no-coverage-upload 添加到 pytest 行的末尾修复了问题