在 Azure DevOps 中,您能否在另一个阶段获得测试结果 - 即总测试 run/passed/failed/ignored?

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

我在 YAML 中有一个 Azure DevOps 管道 运行。

我正在使用 VSTest@2 任务执行一些单元测试。这一切都很好,我看到测试结果出现在舞台概述 UI 本身,以及 header.

的 'Tests and Coverage' 概述中

我的 YAML 管道还会向 Slack 频道发布一条消息,其中包含指向构建、success/failure 状态和其他内容的链接。我也很想将测试结果添加到消息中……只是一个简单的 'Total tests X - Passed X - Failed X - Skipped X' 显示。这发生在最后一个单独的阶段。

有没有办法在管道的后期(运行 在不同的代理上)获得前一阶段的测试结果?

测试是否可作为工件使用?如果是,它们在哪里,采用什么格式?

我认为唯一的方法是通过 Azure API 是否正确? (我真的懒得为这个功能在管道中设置身份验证,我不与 API 交互,但在其他任何地方)

使用VSTest@2任务执行一些测试应该会生成测试结果。您可以查看 VSTest 任务的任务日志,查看测试结果文件输出到哪里。通常默认的测试结果是trx文件。您可以通过将 resultsFolder: 'output location' 添加到 vstest 任务来更改输出位置。

获得测试结果文件后,您可以通过添加脚本任务编写脚本来提取测试结果摘要。

对于下面的示例,使用 powershell 脚本从 trx 文件和 set it to env variable 中提取测试摘要,使其可用于以下任务。

- 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()

为了让变量testSummary在后面的阶段可用,那么需要在后面的阶段添加对这个阶段的依赖。并使用表达式 dependencies.<Previous stage name>.outputs['<name of the job which execute the task.setvariable >.TaskName.VariableName'] 将测试摘要传递给以下阶段的变量。

请检查下面的例子

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:

然后你可以通过引用变量 $(testInfo) 来获取提取的测试结果信息。

希望以上内容对您有所帮助!