Techromancer
Techromancer

Reputation: 493

Getting Tests Run on Build with Azure DevOps REST APIs

Is there a REST API for getting the number of tests run/failed for a build?

I see there's one for getting Code Coverage but unless I am missing something obvious I can't find an API for just getting the number of tests run for a build.

Looks like that all APIs available to get Test Results require a test runid, but I only have a buildid.

Upvotes: 2

Views: 1833

Answers (3)

Levi Lu-MSFT
Levi Lu-MSFT

Reputation: 30373

You can use the Runs Query Api as Matt mentioned. However i found the buildId query parameter didnot work probably. You may need to filter the api results by the buildId. See below example:

$connectionToken="Personal Access Token"

$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))

$url = "https://dev.azure.com/{organization}/{project}/_apis/test/runs?minLastUpdatedDate=2020-10-20&maxLastUpdatedDate=2020-10-22&api-version=6.0"

$results= Invoke-RestMethod -Uri $trurl -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get

$run = $results.value | Where-Object{$_.buildConfiguration.id -eq $buildId}

$runId = $run.id

You can also check out Runs List Api. I found the buildId was always appended to the run title. You can filter api results by the run name. See below:

enter image description here

$connectionToken="Personal Access Token"

$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))

$url = "https://dev.azure.com/{organization}/{project}/_apis/test/runs?api-version=6.0"

$results= Invoke-RestMethod -Uri $trurl -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get

$run = $results.value | Where-Object{$_.name -match $buildId}

$runId = $run.id

Upvotes: 0

Matt
Matt

Reputation: 4065

You should try to use the Runs - Query API. Pass the optional build id.

GET https://dev.azure.com/{organization}/{project}/_apis/test/runs?minLastUpdatedDate={minLastUpdatedDate}&maxLastUpdatedDate={maxLastUpdatedDate}&buildIds={buildIds}&api-version=6.0

It's worth noting that you can customize the title of the run (with the build number included) by setting that on the task step of the pipeline.

Upvotes: 1

Krzysztof Madej
Krzysztof Madej

Reputation: 40899

I'm not sure if there is endpoint dedicated for tests but you can get tests from logs

$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$(System.AccessToken)")) }

$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

You need to pass your build id and log id. To get log id please get all logs and go trough them and find the one with your task with tests.

In terms of buildId vs runId, they are the same. I mean buildId = runId. With newer syntax there were a change in nomenclature.

Upvotes: 0

Related Questions