How to associate test runs with ADO test plan,test suite and test cases

Morkal, Ashvini 0 Reputation points
2024-05-29T09:28:07.82+00:00

We are using the following script in the Azure CLI task in the pipeline, but patch calls failing.

We are trying to associate Test Runs with the ADO Test Plan, Test Suite, and Test Case.

I would need help if we are making any mistakes in API syntax and payload.

$project = "Procurement"

$orgUrl = "https://dev.azure.com/thegordiangroup"

  **$testPlanId = 158920 # Replace with your Test Plan ID**

  **$testSuiteId = 158921 # Replace with your Test Suite ID**

  **$pat = "zfoing2wigd7ptle3i4ypznx7rp5c5ssh6y4uum6e5yrhipiqapq"  # Use Azure DevOps Personal Access Token**



  **# Base64 encode the PAT**

  **$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat"))**



  **# Get the test run ID from the published test results**

  **Write-Output "Fetching test runs..."**

  **$testRunName = "Run By Pipeline01"**

  **$testRuns = Invoke-RestMethod -Uri "$orgUrl/$project/_apis/test/runs?api-version=6.0" -Headers @{Authorization=("Basic $base64AuthInfo")} -Method Get**

  **$testRun = $testRuns.value | Where-Object { $_.name -eq $testRunName }**

 **Write-Output $testRun**

  **if (-not $testRun) {**

      **Write-Error "Test run with name '$testRunName' not found."**

      **exit 1**

  **}**

$testRunId = $testRun.id

  **Write-Output "Found Test Run ID: $testRunId"**



  **# Associate the test run with a test plan and test suite**

  **Write-Output "Associating test run with test plan and test suite..."**

  **$updateTestRunBody = @{**

      **plan = @{**

          **id = $testPlanId**

      **}**

      **testSuite = @{**

          **id = $testSuiteId**

      **}**

  **} | ConvertTo-Json -Depth 3**

#https://dev.azure.com/thegordiangroup/Procurement/_TestManagement/Runs?runId=1038895&_a=runCharts

# Invoke-RestMethod -Uri "$orgUrl/$project/_apis/test/runs/$testRunId?api-version=6.0" -Method Patch -Headers @{Authorization=("Basic

  **Invoke-RestMethod -Uri "$orgUrl/$project/_TestManagement/Runs?runId=$testRunId&_a=runCharts" -Method Patch -Headers @{Authorization=("Basic $base64AuthInfo")} -Body $updateTestRunBody -ContentType "application/json"**

  **Write-Output "Test run associated successfully."**



  **# Get the test results from the test run**

  **Write-Output "Fetching test results..."**

  **$testResults = Invoke-RestMethod -Uri "$orgUrl/$project/_apis/test/runs/$testRunId?api-version=6.0" -Method Get -Headers @{Authorization=("Basic $base64AuthInfo")}**



  **# Update each test result to associate with specific test cases**

  **Write-Output "Updating test results to associate with test cases..."**

  **foreach ($testResult in $testResults.value) {**

      **$testCaseId = Get-TestCaseIdForResult $testResult  # Replace with your logic to map test result to test case ID**

Write-Output "Updating test result ID $($testResult.id) with test case ID $testCaseId..."

      **$updateTestResultBody = @{**

          **testCase = @{**

              **id = $testCaseId**

          **}**

      **} | ConvertTo-Json -Depth 3** 

 **}**

Invoke-RestMethod -Uri "$orgUrl/$project/_apis/test/runs/$testRunId/results/$($testResult.id)?api-version=6.0" -Method Post -Headers @{Authorization=("Basic $base64AuthInfo")} -ContentType "application/json" -Body $updateTestResultBody

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
37,798 questions
{count} votes