在Azure DevOps中,您能否在另一个阶段获得测试结果——即运行/通过/失败/忽略的总测试?

Iai*_*ord 1 nunit unit-testing azure-devops azure-pipelines

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

我正在使用 VSTest@2 任务来执行一些单元测试。这一切都工作正常,我看到测试结果出现在阶段概述 UI 本身以及标题中的“测试和覆盖范围”概述中。

我的 YAML 管道还会向 Slack 通道发布一条消息,其中包含指向构建、成功/失败状态和其他内容的链接。我也热衷于将测试结果添加到消息中...只是一个简单的“总测试 X - 通过 X - 失败 X - 跳过 X”显示。这发生在最后的一个单独的阶段。

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

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

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

Lev*_*SFT 5

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

在此输入图像描述

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

对于下面的示例,使用 powershell 脚本从 trx 文件中提取测试摘要并将其设置为 env 变量,这使其在以下任务中可用。

- 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()
Run Code Online (Sandbox Code Playgroud)

为了使该变量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:
Run Code Online (Sandbox Code Playgroud)

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

希望以上有帮助!