Iain Stanford
Iain Stanford

Reputation: 707

In Azure DevOps, can you get the test results in another Stage - namely total tests run/passed/failed/ignored?

I have an Azure DevOps pipeline running in YAML.

I'm using the VSTest@2 task to execute some unit tests. This is all working fine and I see the test results appear in the Stage overview UI itself, and in the 'Tests and Coverage' overview in the header.

My YAML pipeline also posts a message to a Slack channel with links to the build, success/failure status and other things. I'm keen to add Test results into the message too...just a simple 'Total tests X - Passed X - Failed X - Skipped X' display. This happens in a separate Stage at the end.

Is there a way to get the tests results from a previous stage in a later stage in the pipline (running on a different agent)?

Are the tests available as an artifact, and if so where are they and in what format?

Would I be right in thinking the only way to do this is via the Azure API? (I can't really be bothered to setup auth with that in the pipeline just for this feature, I don't interact with the API yet anywhere else)

Upvotes: 1

Views: 3494

Answers (1)

Levi Lu-MSFT
Levi Lu-MSFT

Reputation: 30313

The test results should be generated if you use VSTest@2 task to execute some tests. You can check the task log of VSTest task to check where the test result file is output to. Usually the default test result is trx file. You can change the output location by adding resultsFolder: 'output location' to the vstest task.

enter image description here

Once you get the test result file, you can write scripts to extract the test result summary by adding a script task.

For below example, use powershell script to extract the test summay from trx file and set it to env variable, which make it available in the following task.

- powershell: |
   #get the path of the trx file from the output folder.
   $path = Get-ChildItem -Path $(Agent.TempDirectory)\TestResults -Recurse -ErrorAction SilentlyContinue -Filter *.trx |  Where-Object { $_.Extension -eq '.trx' }

   $appConfigFile = $path.FullName  #path to test result trx file
   #$appConfigFile = '$(System.DefaultWorkingDirectory)\Result\****.trx' #path to test result trx file

   $appConfig = New-Object XML 
   $appConfig.Load($appConfigFile) 
   $testsummary = $appConfig.DocumentElement.ResultSummary.Counters | select total, passed, failed, aborted

   echo "##vso[task.setvariable variable=testSummary]$($testsummary)" #set the testsummary to environment variable


  displayName: 'GetTestSummary'

  condition: always()

In order to make the variable testSummary available in the following stage, Then you need to add dependency on this stage to the following stage. And use expression dependencies.<Previous stage name>.outputs['<name of the job which execute the task.setvariable >.TaskName.VariableName'] to pass the test summary to the variable in the following stages.

Please check below example

stages: 
  - stage: Test
    displayName: 'Publish stage'
    jobs:
    - job: jobA
      pool: Default
  ...
    - powershell: |
        #get the path of the trx file from the output folder.
        $path = Get-ChildItem -Path $(Agent.TempDirectory)\TestResults -Recurse -ErrorAction SilentlyContinue -Filter *.trx |  Where-Object { $_.Extension -eq '.trx' }

        $appConfigFile = $path.FullName  #path to test result trx file
        #$appConfigFile = '$(System.DefaultWorkingDirectory)\Result\****.trx' #path to test result trx file

        $appConfig = New-Object XML 
        $appConfig.Load($appConfigFile) 
        $testsummary = $appConfig.DocumentElement.ResultSummary.Counters | select total, passed, failed, aborted

        echo "##vso[task.setvariable variable=testSummary]$($testsummary)" #set the testsummary to environment variable

      displayName: 'GetTestSummary'

      condition: always()

  - stage: Release
      dependsOn: Test
      jobs:

      - job: jobA
        variables:
          testInfo: $[dependencies.Test.outputs['jobA.GetTestSummary.testSummary']]

        steps:

Then you can get the extracted test results info by referencing the variable $(testInfo).

Hope above helps!

Upvotes: 5

Related Questions