You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure Pipeline中合并多作业的代码覆盖率?

问题:Azure Pipeline拆分并行作业后代码覆盖率无法合并

单Runner按顺序执行(环境配置>构建>单元测试>e2e测试)时,单元测试与e2e测试的覆盖率可正常合并展示。为提升运行速度,将测试任务拆分为两个并行作业:

  • 作业1:拉取代码、环境配置→单元测试→PublishCodeCoverageResults@2
  • 作业2:拉取代码、环境配置→构建→Playwright e2e测试→PublishTestResults@2

拆分后仅显示Playwright的代码覆盖率,单作业时的正常合并效果丢失。


当前YAML配置

trigger:
  - main
  - release/*

pool:
  vmImage: ubuntu-latest

stages:
  - stage: Validation
    displayName: Validation
    jobs:
      - job: run_e2e_tests
        displayName: Validate and Build
        pool:
          vmImage: ubuntu-latest
        steps:
          - task: Npm@1
            displayName: Install bun
            inputs:
              command: custom
              customCommand: install -g bun@1.1.18

          - script: |
              bun install --frozen-lockfile
            displayName: 'Install dependencies'

          - script: bun run build
            displayName: Build project

          - script: bun run playwright install --with-deps
            displayName: Setup playwright for e2e tests
            # Continue even if the tests fail, so that the e2e tests can be run, and the test results can be published.
            # The PublishTestResults task will fail if there are any failed tests.
            continueOnError: true

          - script: bun run e2e --reporter=junit
            displayName: e2e tests
            # Continue even if the tests fail, so that the test results can be published.
            # The PublishTestResults task will fail if there are any failed tests.
            continueOnError: true
            env:
              CI: true
              PLAYWRIGHT_JUNIT_OUTPUT_NAME: junit-e2e.xml
          - task: PublishTestResults@2
            displayName: Publish test results
            inputs:
              testResultsFormat: JUnit
              testResultsFiles: junit*.xml
              failTaskOnFailedTests: true
              failTaskOnFailureToPublishResults: true
              failTaskOnMissingResultsFile: true

      - job: run_unit_tests
        displayName: Run Unit tests
        pool:
          vmImage: ubuntu-latest
        steps:
          - task: Npm@1
            displayName: Install bun
            inputs:
              command: custom
              customCommand: install -g bun@1.1.18

          - script: |
              bun install --frozen-lockfile
            displayName: 'Install dependencies'

          - script: bun run test --coverage --reporters=jest-junit
            displayName: Unit tests
            # Continue even if the tests fail, so that the e2e tests can be run, and the test results can be published.
            # The PublishTestResults task will fail if there are any failed tests.
            continueOnError: true

          - task: PublishCodeCoverageResults@2
            displayName: Publish unit test code coverage
            inputs:
              summaryFileLocation: $(System.DefaultWorkingDirectory)/coverage/clover.xml
              mergeTestResults: true
              failIfCoverageEmpty: true

  - stage: ArtefactGeneration
    displayName: Artefact Generation
    dependsOn: Validation
    jobs:
      - job: PublishSourceCode
        displayName: Publish Source Code
        steps:
          - task: PublishPipelineArtifact@1
            displayName: Publish Source Code Artefact
            inputs:
              targetPath: $(Pipeline.Workspace)
              artifact: frontend-source-code
              publishLocation: pipeline

注:依赖安装仅需10秒,无需在作业间共享制品。


单作业时的对比YAML配置

trigger:
  - main
  - release/*

pool:
  vmImage: ubuntu-latest

stages:
  - stage: Validation
    displayName: Validation
    jobs:
      - job: StaticValidationAndTests
        displayName: Static Validation and Tests
        steps:
          - task: NodeTool@0
            displayName: Install Node.js
            inputs:
              versionSource: spec
              versionSpec: 20.x
          - task: Npm@1
            displayName: Install bun
            inputs:
              command: custom
              customCommand: install -g bun@1.1.16
          - script: bun install --frozen-lockfile
            displayName: Install dependencies
          - script: bun run build
            displayName: Build project
          - script: bun run lint
            displayName: Run linter
          - script: bun run test --coverage --reporters=jest-junit
            displayName: Unit tests
            # Continue even if the tests fail, so that the e2e tests can be run, and the test results can be published.
            # The PublishTestResults task will fail if there are any failed tests.
            continueOnError: true
          - task: PublishCodeCoverageResults@2
            displayName: Publish unit test code coverage
            inputs:
              summaryFileLocation: $(System.DefaultWorkingDirectory)/coverage/clover.xml
              failIfCoverageEmpty: true
          - script: bun run playwright install --with-deps
            displayName: Install playwright
            enabled: false
          - script: bun run e2e --reporter=junit
            displayName: e2e tests
            # Continue even if the tests fail, so that the test results can be published.
            # The PublishTestResults task will fail if there are any failed tests.
            continueOnError: true
            enabled: false
            env:
              CI: true
              PLAYWRIGHT_JUNIT_OUTPUT_NAME: junit-e2e.xml
          - task: PublishTestResults@2
            displayName: Publish test results
            inputs:
              testResultsFormat: JUnit
              testResultsFiles: junit*.xml
              mergeTestResults: true
              failTaskOnFailedTests: true
              failTaskOnFailureToPublishResults: true
              failTaskOnMissingResultsFile: true
  - stage: ArtefactGeneration
    displayName: Artefact Generation
    dependsOn: Validation
    jobs:
      - job: PublishSourceCode
        displayName: Publish Source Code
        steps:
          - task: PublishPipelineArtifact@1
            displayName: Publish Source Code Artefact
            inputs:
              targetPath: $(Pipeline.Workspace)
              artifact: frontend-source-code
              publishLocation: pipeline

内容的提问来源于stack exchange,提问作者Umair Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 04:36:06