在 Python 中合并来自多个 Azure Pipeline 作业的覆盖结果(使用 pytest)

sho*_*yer 6 azure-devops azure-pipelines

我已将我的开源项目设置为使用 Azure Pipelines 运行 CI,并按照 Azure 管道文档中有关如何测试 Python 应用程序的示例收集代码覆盖率。

这似乎工作得很好,但代码覆盖率统计似乎只从单个作业(随机)中获取测试结果。为了获得我的项目的完整覆盖率(例如,对于平台相关代码),我确实需要聚合所有测试作业的覆盖率。

以下是我的管道中的相关任务:

- bash: |
    source activate test_env
    pytest xarray --junitxml=junit/test-results.xml \
    --cov=xarray --cov-config=ci/.coveragerc --cov-report=xml
  displayName: Run tests
- task: PublishCodeCoverageResults@1
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml'
    reportDirectory: '$(System.DefaultWorkingDirectory)/**/htmlcov'
Run Code Online (Sandbox Code Playgroud)

配置 Azure 以显示此信息的正确方法是什么?

我试过添加--cov-append到我的pytest调用中,但这似乎没有什么区别。

Bru*_*uno 1

根据文档,当您从不同的作业运行多个测试时,每次作业后都会清理结果,因此如果您想将所有测试结果收集到一个地方,则必须将结果发布到工件并在所有测试作业结束时发布,你必须整合它们。

基本上对于每个测试你都会有:

- jobs:
  - job: testjob1
    steps:
    - step1 -> run tests
    - step2 -> publish job1 artifacts
  - job: testjob2
    steps:
    - step1 -> run tests
    - step2 -> publish job2 artifacts
   ....
  - job: consolidate
    steps:
    - step1 -> downloadArtifacts
    - step2 -> publish test results
Run Code Online (Sandbox Code Playgroud)

最后,您需要一份工作来下载所有这些工件,他们会像您一样发布它们。

参考:https://learn.microsoft.com/en-us/azure/devops/pipelines/process/phases? view=azure-devops&tabs=yaml

参考:https://learn.microsoft.com/en-us/azure/devops/pipelines/artifacts/pipeline-artifacts ?view=azure-devops&tabs=yaml