Search code examples
azure-devopscmake

How to show test results when pipeline fails


In my pipeline there is a job that fails. This failure is expected and the pipeline should be continued when the error is detected. The pipeline runs and publishes unit tests I have this working, the pipeline runs to the end and tests are executed and published. However, the test results are not displayed and the pipeline is marked as failed.

Here is the full yaml

trigger: none
# Variable to store where the initial code checkout path
variables:
- name: BuildDir
  value: Test

stages:

  - stage: Checkout
    pool: $(PoolName)
    jobs:
      - job: CheckoutRedwoodAndSealink
        displayName: Checking out
        steps:
          - checkout: git://Redwood/Redwood@working/conifer
            path: 'Redwood'
            displayName: Checking out Redwood
          - checkout: self
            path: 'Sealink'
            displayName: Checking out Sealink
          - task: PowerShell@2
            displayName: Change the value of BuildDir
            # Store the checkout location
            inputs:
              targetType: 'inline'
              script: 'Write-Host "##vso[task.setvariable variable=BuildDir;isOutput=true]$(Agent.BuildDirectory)"'
            name: setvar 

  - stage: CopyThirdParty
    dependsOn: Checkout
    pool: $(PoolName)
    variables:
      # Set the build directory
      BuildDir: $[stageDependencies.Checkout.CheckoutRedwoodAndSealink.outputs['setvar.BuildDir'] ]

    jobs:
      - job: CopyRedwoodTP
        displayName: Copying Redwood Third Party 
        steps:
        - checkout: none
        - task: CmdLine@2
        - task: CopyFiles@2
          inputs:
            SourceFolder: 'C:\Redwood\third-party'
            Contents: '**'
            TargetFolder: '$(BuildDir)\Redwood\third-party'
            OverWrite: true

  - stage: Build
    dependsOn: 
      - Checkout
      - CopyThirdParty
    pool: $(PoolName)
    variables:
      # Set the build directory
      BuildDir: $[stageDependencies.Checkout.CheckoutRedwoodAndSealink.outputs['setvar.BuildDir'] ]
    jobs:      
    - job: Configure
      displayName: Configuring CMake
      steps:
        - checkout: none
        
        - task: CMake@1
          inputs:
              cmakeArgs: -S ./ -B ./build
              workingDirectory: $(BuildDir)/Sealink
        - task: CMake@1
          inputs:
              cmakeArgs: -S ./ -B ./build
              workingDirectory: $(BuildDir)/Sealink
    - job: Build
      displayName: Building
      dependsOn: Configure
      steps:
        - checkout: none
        
        - task: CMake@1
          inputs:
              cmakeArgs: '--build ./build --config Debug'
              workingDirectory: $(BuildDir)/Sealink
          continueOnError: true
    # if first build failed - try again      
    - job: Build2
      condition: failed()
      dependsOn: Build 
      steps:
        - checkout: none 
        - task: CMake@1
          displayName: Configuring CMake )2)
          inputs:
              cmakeArgs: -S ./ -B ./build
              workingDirectory: $(BuildDir)/Sealink
        - task: CMake@1
          displayName: Building (2)
          inputs:
              cmakeArgs: '--build ./build --config Debug'
              workingDirectory: $(BuildDir)/Sealink

  - stage: RunTests
    pool: $(PoolName)
    dependsOn: 
      - Build
      - Checkout
    variables:
      BuildDir: $[stageDependencies.Checkout.CheckoutRedwoodAndSealink.outputs['setvar.BuildDir'] ]
    jobs:
      - job: RunTests
        displayName: Running Unit Tests
        continueOnError: true
        steps:
        - checkout: none
        - task: PowerShell@2
          inputs:
            targetType: 'inline'
            script: '$(BuildDir)/Sealink/build/UnitTesting/Debug/Test.exe --gtest_output="xml:./TestResults/"'
            
  - stage: Publish
    pool: $(PoolName)
    dependsOn: 
      - RunTests
      - Checkout
    jobs:
      - job: publish
        
        steps:
        - checkout: none
        - task: PublishTestResults@2
          displayName: Publishng unit test results
          inputs:
            testResultsFormat: 'JUnit'
            testResultsFiles: '*.xml'
            searchFolder: '$(System.DefaultWorkingDirectory)/TestResults'
            failTaskOnFailedTests: true              
              

By using continueOnError the pipeline continues to run but no test results are displayed

enter image description here


Solution

  • Make sure your test has been run and the test result has been generated successfully. Below is my yaml for your reference.

    steps:
    - script: |
        cmake -B build && cmake --build build
      displayName: 'Build'
    
    - script: |
        cmake -E chdir build ctest -T Test
      displayName: 'Test'
    
    - task: PublishTestResults@2
      inputs:
        testResultsFormat: 'CTest'
        testResultsFiles: '**/Test.xml'
    
    - task: PublishBuildArtifacts@1
      inputs:
        PathtoPublish: '$(Agent.BuildDirectory)'
        ArtifactName: 'drop'
        publishLocation: 'Container'
    

    The test result:

    • In the log of Test step: enter image description here
    • In the Summary page: enter image description here

    In the yaml you shared, there is only build but no test. You can use the -T option followed by ctest to generate the test results file Test.xml. This file will be generated in the Testing directory under the current build directory. You can add a PublishBuildArtifacts@1 task in your pipeline to check further.