首页 > 解决方案 > Azure DevOps Pipeline:显示 python 项目的代码覆盖率报告

问题描述

我试图让代码覆盖率结果显示在管道运行摘要选项卡上。为了调试它,我使用了一个带有以下 pipeline.yaml 文件的示例项目:

trigger:
- master

pool:
  vmImage: ubuntu-latest

steps:
- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.7'
  displayName: 'Use Python 3.7'

- script: |
    pip install -r requirements.txt
  displayName: 'Install requirements'


- script: |
    pip install pytest pytest-azurepipelines
    pip install pytest-cov
    pytest --doctest-modules --junitxml=junit/test-results.xml --cov=. --cov-report=xml
  displayName: 'pytest'

- task: PublishTestResults@2
  condition: succeededOrFailed()
  inputs:
    testResultsFiles: '**/test-*.xml'
    testRunTitle: 'Publish test results'

PublishTestResults 任务的输出是(编辑:正如答案所指出的,这与问题无关,因为它只报告测试上传,而不是代码覆盖报告上传):

Starting: PublishTestResults
==============================================================================
Task         : Publish Test Results
Description  : Publish test results to Azure Pipelines
Version      : 2.180.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/test/publish-test-results
==============================================================================
/usr/bin/dotnet --version
5.0.301
Result Attachments will be stored in LogStore
Run Attachments will be stored in LogStore
Async Command Start: Publish test results
Publishing test results to test run '324'.
TestResults To Publish 1, Test run id:324
Test results publishing 1, remaining: 0. Test run id: 324
Published Test Run : https://dev.azure.com/.../Runs?runId=324&_a=runCharts
Async Command End: Publish test results
Finishing: PublishTestResults

因此,似乎一切都按预期进行。但是,我没有在摘要选项卡上显示结果(并且没有出现代码覆盖率选项卡): 管道摘要的屏幕截图

由于我没有想法可以尝试什么:此功能是否损坏或我做错了什么?

编辑:建议添加一个 PublishCodeCoverageResults 任务。但是,在上面的 yaml 文件末尾有以下内容,它仍然不起作用。

- task: PublishCodeCoverageResults@1
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml'

任务的输出是:

Starting: PublishCodeCoverageResults
==============================================================================
Task         : Publish code coverage results
Description  : Publish Cobertura or JaCoCo code coverage results from a build
2021-07-21T04:26:34:  -reports:/home/vsts/work/1/s/**/coverage.xml
2021-07-21T04:26:34:  -targetdir:/home/vsts/work/_temp/cchtml
2021-07-21T04:26:34:  -reporttypes:HtmlInline_AzurePipelines
2021-07-21T04:26:35: Writing report file '/home/vsts/work/_temp/cchtml/index.html'
2021-07-21T04:26:35: Report generation took 0.5 seconds
Generated code coverage html report: /home/vsts/work/_temp/cchtml
Reading code coverage summary from '/home/vsts/work/1/s/coverage.xml'
Async Command Start: Publish code coverage
Publishing coverage summary data to TFS server.
 Lines- 17 of 22 covered.
 Branches- 0 of 0 covered.
Modifying Cobertura Index file
Publishing code coverage files to TFS server.
Uploading 8 files
Building file tree
Uploaded 0 out of 1,426,385 bytes.
Uploaded 1,426,385 out of 1,426,385 bytes.
Associating files
Total files: 8 ---- Associated files: 0 (0%)
File upload succeed.
Published '/home/vsts/work/_temp/cchtml' as artifact 'Code Coverage Report_1263'
Async Command End: Publish code coverage
Finishing: PublishCodeCoverageResults

标签: azure-devopsazure-pipelinescode-coverageazure-pipelines-yamlbuild-pipeline

解决方案


要查看代码覆盖率,我认为您需要添加任务 PublishCodeCoverageResults@1

https://docs.microsoft.com/en-us/azure/devops/pipelines/ecosystems/python?view=azure-devops#run-tests


推荐阅读