Azure DevOps Pipeline:显示 python 项目的代码覆盖率报告

问题描述 投票:0回答:4

我正在尝试让代码覆盖率结果显示在管道运行摘要选项卡上。为了调试这个问题,我使用了一个带有以下 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://learn.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

所以,看起来一切都按预期进行。但是,我没有在摘要选项卡上显示结果(并且没有出现代码覆盖率选项卡): Screenshot of pipeline summary

因为我没有想法要尝试什么:这个功能是否损坏或者我做错了什么?

编辑: 建议添加 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-devops azure-pipelines code-coverage azure-pipelines-yaml build-pipeline
4个回答
2
投票

添加发布代码覆盖率结果任务。

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

Estensionhttps://marketplace.visualstudio.com/items?itemName=dazfuller.pyunittest-task


1
投票

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

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


0
投票

您可以使用 Coverage.py 生成

coverage.xml
报告文件。之后,可以使用 PublishCodeCoverageResults@2 任务发布它。这是
azure-pipelines.yaml
文件的代码片段:

    steps:
    - task: UsePythonVersion@0
      inputs:
        versionSpec: '3.10'
    - script: python -m pip install --upgrade pip setuptools
      displayName: Upgrade pip and setuptools
    - script: python -m pip install -r requirements.txt
      displayName: Install all dependencies
    - script: pre-commit run --all-files
      displayName: Check code quality
    - script: |
        coverage run -m pytest
        coverage xml -o coverage.xml
      displayName: Generate coverage report
    - task: PublishCodeCoverageResults@2
      inputs:
        summaryFileLocation: '**/coverage.xml'

首先,生成覆盖率报告任务创建

.coverage
文件,并从中创建报告。 PublishCodeCoverageResults 任务的第二个版本需要一个必需参数,即
summaryFileLocation
字段。

接下来,您可以在Pipelines菜单选项下的Code Coverage选项卡上查看覆盖率报告。


0
投票

您在 Azure 中使用coverage.py 的体验如何?因为 Cobertura 拥有有关每个文件的代码覆盖率的特定信息。

PS 我能够让 Cobertura 工作,但需要进行一些本地测试才能查看使用此包时文件的位置。最后这对我来说是一条路:

summaryFileLocation: $(System.DefaultWorkingDirectory)/**/coverage.xml
reportDirectory: $(System.DefaultWorkingDirectory)/**/htmlcov
© www.soinside.com 2019 - 2024. All rights reserved.