How can I get the last test run id from TFS to my

2019-07-22 08:25发布

I would like to use the very last TestRunId in case of create a report via powershell which sends the infos to Slack. I use Team Services REST API to get the test results. It works fine but only with specific Run ID. Here is a link where you can find good examples: enter link description here
I'm stuck at finding a way to get the last test result ID which I would use in my GET request to the TFS REST API:

Invoke-RestMethod -Uri "https://{instance}/DefaultCollection/{project}/_apis/test/runs/{run}/results?api-version={version}[&detailsToInclude={string}&$skip={int}&$top={int}]"

So I find {run} as the last Test Run ID with no luck.

Anyone have an idea? I cant find any query language which can be use in this situation in a powershell script.

1条回答
Ridiculous、
2楼-- · 2019-07-22 09:09

You could retrieve the list of test runs, the sort descending the result on ID, since the most recent test run has the greatest ID. Then get the first item of the result. All of this shown below in powershell:

$username = "doesnotmatter" 
$token = "PUTYOURTOKEN_HERE" 
$instance = "PUTYOURACCOUNTHERE.visualstudio.com" 
$teamProjectName = "PUTHEREYOUR_TEAM_PROJECT_NAME"

#create auth header to use for REST calls 
$accessToken = ("{0}:{1}" -f $username,$token) 
$accessToken = [System.Text.Encoding]::UTF8.GetBytes($accessToken) 
$accessToken = [System.Convert]::ToBase64String($accessToken) 
$headers = @{Authorization=("Basic {0}" -f $accessToken)} 

$testRuns = Invoke-RestMethod -Uri "https://$instance/defaultcollection/$(teamProjectName)/_apis/test/runs/?api-version=3.0-preview" -Headers $headers -Method Get 

$testRunsIdSorted = $testRuns.value | sort-object id -Descending

$mostRecentTestRun = Invoke-RestMethod -Uri "https://$instance/defaultcollection/$(teamProjectName)/_apis/test/runs/$($testRunsIdSorted[0].id)?api-version=3.0-preview" -Headers $headers -Method Get 

$mostRecentTestRun is now the most recent test run. Note that the script does no error checking at all. Note that for authentication the script is using a Personal Access Token that needs to have the Test management (read) scope at least.

查看更多
登录 后发表回答