【问题标题】:How to get the unit test results in variables in Azure DevOps Pipelines?如何在 Azure DevOps Pipelines 中获取变量中的单元测试结果?
【发布时间】:2020-07-09 23:13:12
【问题描述】:

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

我需要在变量中获取单元测试的结果。例如,如果有 10 个测试用例,两个失败,我需要得到类似:

failedTestCases = 2
succeededTestCases = 8

这是因为我在接下来的任务中需要这些值。有没有办法做到这一点?

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

【问题讨论】:

    标签: azure unit-testing azure-devops azure-pipelines


    【解决方案1】:

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

    • 测试应用
    • 获取测试详情
    • 显示测试详情

    对你来说,非常重要的部分是弄清楚你有哪个日志 ID 用于你的测试。基本上,如果您的测试任务在此列表中排名第五(包括Initialize job):

    您需要添加 3 并且您有您的 logId。就我而言,这是 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()
    

    为此我得到了:

    所以这意味着我们在变量中有许多通过和失败的测试可供使用。

    【讨论】:

    • 您好 Krzysztof Madej,感谢您的回答,我真的很欣赏它,我可以使用它。我做了一些测试,解决方案很奏效。我需要对其进行一些调整。但我并不完全喜欢我需要“强制”任务的位置编号的部分,因为如果您移动任务或将其他任务添加到管道中,它很容易改变。您知道是否有办法从变量中获取该数字。
    • 我检查了我在使用您的解决方案时遇到的问题,这是因为我的日志是西班牙语的,所以我在寻找“Correcto”而不是“Passed”和“Incorrecto”而不是“Failed” “,所以这两个词都包含“correcto”,我在两个变量中都得到了失败的测试。我刚刚修好了它,它工作得很好。尽管如此,我还是有点担心“强制”任务的位置编号。我一直在互联网上搜索,找不到任何东西来获得这个价值。
    • 这是有可能的。例如,您可以使用 REST API 根据名称获取 Id。但是,如果您更改名称,则整个管道都会失败。
    • 我认为这样更好。我正在检查 REST API 文档,但找不到任何端点来按名称获取任务,这正是我想要的。很抱歉再次问您,但是,您知道哪个是端点名称吗?
    • 请使用endpoint
    猜你喜欢
    • 1970-01-01
    • 2021-05-31
    • 2021-03-06
    • 2022-07-20
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 2021-06-25
    • 2015-01-08
    相关资源
    最近更新 更多