如何从 TFS 获取最后一次测试运行 ID 到我的 powershell 脚本 [英] How can I get the last test run id from TFS to my powershell script

查看:17
本文介绍了如何从 TFS 获取最后一次测试运行 ID 到我的 powershell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用最后一个 TestRunId,以防通过 powershell 创建报告,该报告将信息发送到 Slack.我使用 Team Services REST API 来获取测试结果.它工作正常,但仅适用于特定的运行 ID.这是一个链接,您可以在其中找到很好的示例:输入链接描述这里
我一直在寻找一种方法来获取最后一个测试结果 ID,我将在对 TFS REST API 的 GET 请求中使用该 ID:

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}]"

所以我发现 {run} 作为最后一个没有运气的测试运行 ID.

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

有人有想法吗?我在 powershell 脚本中找不到可以在这种情况下使用的任何查询语言.

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

推荐答案

您可以检索测试运行列表,按 ID 对结果进行降序排序,因为最近的测试运行具有最大的 ID.然后得到结果的第一项.所有这些都显示在下面的 powershell 中:

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 现在是最近的测试运行.请注意,该脚本根本不进行错误检查.请注意,对于身份验证,脚本使用 个人访问令牌,至少需要具有测试管理(读取)范围.

$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.

这篇关于如何从 TFS 获取最后一次测试运行 ID 到我的 powershell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆