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

前端 未结 1 1565
长情又很酷
长情又很酷 2021-01-21 04:01

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 wo

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-21 04:36

    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.

    0 讨论(0)
提交回复
热议问题