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

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

问题描述

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

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到Powershell脚本获取最后的测试运行ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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