WietzeVeld
WietzeVeld

Reputation: 177

TFS Rest API: parameter outcomes to filter test results is not working

We have been tinkering with the TFS REST API. Our goal is to get test results from a specific test run and only return the failed tests.

In this documentation it states to use the parameter "outcomes", but when we add this parameter the GET operation still returns all test results, including the passed ones.

We are using version 2.0, but we are unable to find the documentation specific for this version.

This is an example of our call.

https://<server>/tfs/<collection>/<project>/_apis/test/Runs/<run id>/results?api-version=2.0&outcomes=Failed

Any help on how this filtering works (or the definitive answer if this does not work for version 2.0) is much appreciated.

Upvotes: 0

Views: 594

Answers (1)

Andy Li-MSFT
Andy Li-MSFT

Reputation: 30432

Tested on my side, seems it's not available for the previous API versions (Tested on TFS 2017 and 2018, both do not work).

The documentation mentioned with the api-version=5.0-preview.5, maybe it will be available on later releases.

However you can filter the test results by outcomes using PowerShell with the REST API, see below sample:

Param(
   [string]$collectionurl = "http://server:8080/tfs/DefaultCollection", 
   [string]$project = "ProjectName",
   [string]$testrunID = 223,
   [string]$user = "username",
   [string]$token = "password"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$baseUrl = "$collectionurl/$project/_apis/test/runs/$testrunID/results?api-version=3.0-preview"
$response = Invoke-RestMethod -Uri $baseUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$results = $response.value | Where {$_.outcome -eq "Failed"} #| Filter the test results by outcomes


$TestResults = @()

foreach ($result in $results)
{

$customObject = new-object PSObject -property @{
          "TestResultID" = $result.id
          "projectName" = $result.project.name
          "testCaseName" = $result.testCase.name
          "startedDate" = $result.startedDate
          "completedDate" = $result.completedDate
          "outcome" = $result.outcome
          "state" = $result.state
          "runBy" = $result.runBy.uniqueName
          "errorMessage" = $result.errorMessage
        } 

    $TestResults += $customObject
}

$TestResults | Select `
                TestResultID,
                projectName, 
                testCaseName,
                startedDate,
                completedDate,
                outcome,
                state,
                runBy,
                errorMessage #|export-csv -Path D:\temp\TestResult.csv -NoTypeInformation # Export to .csv file

enter image description here

Upvotes: 1

Related Questions