首页 > 解决方案 > 只能进行 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}

参考:https ://docs.microsoft.com/en-us/rest/api/azure/devops/test/runs/list?view=azure-devops-rest-5.1#general-example

标签: powershellazure-devops

解决方案


您可以使用 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

希望以上对您有所帮助。


推荐阅读