如何在Azure DevOps Pipelines中的变量中获取单元测试结果? [英] How to get the unit test results in variables in Azure DevOps Pipelines?

查看:64
本文介绍了如何在Azure DevOps Pipelines中的变量中获取单元测试结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Azure DevOps中有一个构建管道,并且我正在使用.NET Core任务来进行单元测试.

I have a build pipeline in Azure DevOps and I'm using the .NET Core task for applying unit testing.

我需要获取变量中单元测试的结果.例如,如果有10个测试用例,而两个失败,那么我需要得到类似的东西:

I need to get the result of the unit tests in variables. For example, if there are 10 tests cases and two failed, I need to get something like:

failedTestCases = 2
succeededTestCases = 8

这是因为在下一个任务中需要这些值.有办法吗?

This is because I need those values in the next tasks. Is there a way to do that?

需要明确的是,我不需要发布结果,它们已经被发布了,我需要在执行时获取这些值.

To be clear, I don't need to publish the results, they are already being published, I need to get those values in execution time.

推荐答案

是的,这是可能的,但我认为您需要使用REST API.您将在下面找到构建定义的一部分.分三个步骤:

Yes, this is possible but in my opinion you need to use REST API. Below you will find part of build definition. There are three steps:

  • 测试应用
  • 获取测试详细信息
  • 显示测试详细信息

对您来说,非常重要的部分是弄清楚您要测试的日志ID.基本上,如果您的测试任务在此列表上位于第五位(包括 Initialize作业):

For you very important part is to figure out which log id you have for your test. Bascially if your test task is on 5th position on this list (including Initialize job):

您需要添加3并具有您的logId.就我而言,这是8.

You need to add 3 and you have your logId. In my case this is 8.

variables:
  devopsAccount : 'thecodemanual'
  projectName : 'DevOps Manual'
  logId: "8"

- task: DotNetCoreCLI@2
  displayName: Test
  inputs:
    command: test
    projects: 'dotnet-core-on-windows/*Tests/*.csproj'
    arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage" -- RunConfiguration.DisableAppDomain=true'
    workingDirectory: $(rootDirectory)

- task: PowerShell@2
  condition: always()
  name: testDetails
  inputs:
    targetType: 'inline'
    script: |
        # Encode the Personal Access Token (PAT)
        $AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$(System.AccessToken)")) }

        # Get a list of releases
        $uri = "https://dev.azure.com/$(devopsAccount)/$(projectName)/_apis/build/builds/$(Build.BuildId)/logs/$(logId)?api-version=5.1"

        Write-Host $uri

        # Invoke the REST call
        $result = Invoke-RestMethod -Uri $uri -Method Get -Headers $AzureDevOpsAuthenicationHeader

        Write-Host $result

        $lines = $result.Split([Environment]::NewLine)

        $passed = 0;
        $failed = 0;

        foreach($line in $lines) {
            if ($line -match "Passed:.(\d+)") { 
              $passed = $matches[1]
            }

            if ($line -match "Failed:.(\d+)") { 
              $failed = $matches[1]
            }
        }

        echo $passed
        echo $failed

        Write-Host "##vso[task.setvariable variable=passed]$passed"
        Write-Host "##vso[task.setvariable variable=failed]$failed"

- script: |
    echo $(passed)
    echo $(failed)
  condition: always()

为此,我得到了:

因此,这意味着我们已经可以使用多个通过测试和失败测试的变量.

So it means we have number of passed and failed tests in variables ready to use.

这篇关于如何在Azure DevOps Pipelines中的变量中获取单元测试结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆