powershell - 只能进行 10 次试运行?
问题描述
我正在尝试从本地 Azure DevOps 服务器获取一些信息。但是,以下代码仅返回 10 个测试。如何获得一个月的测试运行?
$Api = "http://azuredepopsserver/tfs/Default/MyProject/_apis"
$testUrl = "$Api/test/runs?api-version=5.1&`$top=100"
$tests = Invoke-RestMethod $testUrl -UseDefaultCredentials
$tests
顺便说一句,如何获得测试用例计数?
我从 Levi 的回答中尝试了以下代码,
$WorkItemType = "Test Case"
$WIQL_query = "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = '$WorkItemType'"
$body = @{ query = $WIQL_query }
$bodyJson = @($body) | ConvertTo-Json
Invoke-RestMethod -Uri "$($Api)?api-version=5.1-preview" -Method Post -ContentType "application/json" -Body $bodyJson -UseDefaultCredentials
但它得到了错误
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"值不能为空。\r\n参数名称:配置","typeName":"System.ArgumentNullException, mscorlib" ,"typeKey":"ArgumentNullException","errorCode":0,"eventId":0}
解决方案
您可以使用 Test RUN查询 api来获取一个月的测试运行。根据 api,最小和最大日期最多为 7 天。但是我们可以在 powershell 中做一个循环来让测试运行一个月。请检查以下示例:
$mindate = [dateTime]::ParseExact("2019-09-01","yyyy-MM-dd",$null)
$maxdate = [dateTime]::ParseExact("2019-09-30","yyyy-MM-dd",$null)
$connectionToken="PAT"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
For($i = $mindate; $i -lt $maxdate; $i=$i.AddDays(7)){
$min=$i.ToString('yyyy-MM-dd')
if($i.AddDays(7) -gt $mdate){
$max = $mdate.ToString('yyyy-MM-dd')
}else{
$max = $i.AddDays(7).ToString('yyyy-MM-dd')
}
$testurl = "https://dev.azure.com/ORG/PROJECT/_apis/test/runs?minLastUpdatedDate=$min&maxLastUpdatedDate=$max&api-version=5.1"
$result4 = Invoke-RestMethod -Uri $urldate -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get
$result4
}
要获取测试用例计数,您可以使用wiql api。请检查以下示例:
$url = "https://dev.azure.com/org/proj/_apis/wit/wiql?api-version=5.1"
$WorkItemType = "Test Case"
$WIQL_query = "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = '" + $WorkItemType + "'"
$body = @{ query = $WIQL_query }
$bodyJson=@($body) | ConvertTo-Json
$connectionToken="PAT"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$result4 = Invoke-RestMethod -Uri $url -Headers @{authorization = "Basic $base64AuthInfo"} -Method Post -ContentType "application/json" -Body $bodyJson
$result4.workItems.count
希望以上对您有所帮助。
推荐阅读
- sql - 如何使用 SQL Server 将行连接到一个单元格并在每个单元格后添加换行符
- java - 我正在尝试返回一个空列表
- python - 长寿的Python“多平台”推送库
- php - SQL Query 在数据库中有两个值时仅输出一个结果的问题
- php - Laravel 5.7:尚未设置外观根
- firebase - Flutter 依赖于 firebase_analytics 0.0.4,需要 SDK 版本 >=1.8.0 <2.0.0,版本解析失败
- c - 除非我调用 printf() 函数,否则 C 代码会卡住
- python - 在 linux 中从 selenium 启动 chromium 的路径是什么?
- amazon-web-services - 将数据传递给 step 函数 catch
- swift - 如何在 Swift 中将垂直对齐应用于 UIBarButtonItem