我想使用最后一个TestRunId来通过Powershell创建报告,该报告将信息发送到Slack。我使用Team Services REST API来获取测试结果。它可以正常运行,但仅适用于特定的运行ID。您可以在此处找到很好的示例的链接:enter link description here
我一直在寻找一种方法来获取最后的测试结果ID,该ID将用于TFS REST API的GET请求中:
Invoke-RestMethod -Uri "https://{instance}/DefaultCollection/{project}/_apis/test/runs/{run}/results?api-version={version}[&detailsToInclude={string}&$skip={int}&$top={int}]"
因此,我发现{run}是最后一次没有运气的测试运行ID。
有人有主意吗?我在Powershell脚本中找不到在这种情况下可以使用的任何查询语言。
最佳答案
您可以检索测试运行的列表,排序顺序是ID的结果,因为最近的测试运行的ID最大。然后获得结果的第一项。所有这些都显示在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
现在是最新的测试运行。请注意,脚本根本不执行任何错误检查。
请注意,为了进行身份验证,脚本使用的Personal Access Token至少需要具有测试管理(读取)范围。