只能获得10次测试运行?

时间:2019-11-18 22:04:47

标签: powershell azure-devops

我正在尝试从内部的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参数名称:Configuration”,“ 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

1 个答案:

答案 0 :(得分:1)

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

希望以上内容对您有所帮助。