How to collate code coverage from multiple jobs?

Umair Ahmed 0 Reputation points
2024-07-09T19:47:30.19+00:00

Essentially, I have a pipeline, that sort of works how we expect it to. Everything works with a single runner, in a waterfall way.

Setup > Build > Unit tests > e2e tests

Then in 2nd stage, once all that passes, we publish the artefact.

Since Unit and e2e tests are in the same Stage, the reporter correctly logs them in the coverage section and shows e2e and unit tests together.

However, I've tried to split these in two jobs so two parallel runners can run to make the run faster.

The split is,
Job 1:

  • Checks out repo, setup > unit tests > PublishCodeCoverageResults@2

Job 2:

  • Checks out repo, setup > build > e2e suite using Playwright > PublishTestResults@2

Once both these jobs succeed, an Artefact is generated.

These same tasks, when run in a single runner, generate the tests report correctly, however, in a parallel setup, they don't. Only the Playwright code coverage is displayed.

Here is the yaml Im currently working on:

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

Please note that installing deps only take 10 seconds, so I didn't find it necessary to share artefacts between jobs.

Community Center Not monitored
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. VarunTha 14,850 Reputation points Microsoft External Staff Moderator
    2024-07-09T21:39:21.58+00:00

    Hi Umair Ahmed,
    Thanks for the question and using MS Q&A platform.

    Azure DevOps is currently not supported in the Microsoft Q&A platform; the supported products are listed over here https://docs.microsoft.com/en-us/answers/products (more to be added later on).

    In order to assist best on your query, I would request you to post your query in SO => Azure Devops dedicated support. Additionally, adding the [Azure] tag on SO will increase visibility as it is a Microsoft Sponsored tag.

    https://stackoverflow.com/questions/tagged/azure-devops

    OR

    Report any Azure DevOps problems on Developer Community.

    This will assist you with a faster reply to your query.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.