0
votes

I'm trying to get some information from an Azure DevOps server on premise. However, the following code only return 10 tests. How to get the test runs for a month?

$Api = "http://azuredepopsserver/tfs/Default/MyProject/_apis"
$testUrl = "$Api/test/runs?api-version=5.1&`$top=100"
$tests = Invoke-RestMethod $testUrl -UseDefaultCredentials
$tests

BTW, how to get the test cases counts?

I tried the following code from the answer of 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

But it got the error of

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"Value cannot be null.\r\nParameter name: Configuration","typeName":"System.ArgumentNullException, mscorlib","typeKey":"ArgumentNullException","errorCode":0,"eventId":0}

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

1
Hi @ca9163d9 did you get a chance to try out below answer? Please let me know how did it go? We can discuss it to work out a better solution.Levi Lu-MSFT

1 Answers

1
votes

You can use Test RUN query api to get the test runs for a month. According to the api the min and max date can be atmost 7 days. But we can do a loop in the powershell to get the test runs for a month. Please check below example:

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

To get the test cases counts you can use wiql api. Please check below example:

$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

Hope you find above helpful.