How to get the unit test results in variables in Azure DevOps Pipelines?

只愿长相守 提交于 2021-02-11 06:08:42

问题


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

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.


回答1:


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:

  • test app
  • get test details
  • display test details

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

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

And for this I got:

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



来源:https://stackoverflow.com/questions/62825065/how-to-get-the-unit-test-results-in-variables-in-azure-devops-pipelines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!