Search code examples
azureazure-devopsazure-pipelinespipeline

How can i check if some of my work items have a test case failing


How can i check if some of my work items have a test case failing when executing my pipeline in azure devops?enter image description here


Solution

  • How can i check if some of my work items have a test case failing

    The test case has no outcome state(e.g. fail pass ..). The outcome state is for Test points in Test Plan -> Execute tab.

    If we add test case in the Boards and run it, it will automatically create a test plan, test suit and test run, we could check the outcome via them.

    To check if there are failed test points in PBI(Your work item type), we could use PowerShell Task and enter the below script to list the outcome.

    cls
    $PAT="{PAT}"
    $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
    
    
    #Get serviceHost ID
    $serviceHostURL = "https://vssps.dev.azure.com/{Org name}/_apis/graph/users?api-version=6.0-preview.1"
    $serviceHostResult = Invoke-RestMethod -Uri $serviceHostURL -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get
    ForEach ($ID in $serviceHostResult.value){
    
        #filter the link type and get the test case ID
        if($ID.displayName -eq "Project Collection Build Service ({Org name})"){
            $serviceHost = $ID.principalName
        }
    }
    
    
    #List all test case ID
    
    $URL="https://dev.azure.com/{Org name}/{Project name}/_apis/wit/workitems/{Work item ID}?" + "$" + "expand=1&api-version=6.1-preview.3"
    $Result = Invoke-RestMethod -Uri $URL -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get
    $test=$Result.relations.url
    
    ForEach ($ID in $Result.relations){
    
        #filter the link type and get the test case ID
        if($ID.rel -eq "Microsoft.VSTS.Common.TestedBy-Forward"){
           
            $TestCaseID = $ID.url.split("/",9)[-1]
    
            #Get test run ID via test plan ID, test suit ID and test case ID
            $Url = "https://dev.azure.com/{Org name}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview"  
    $Body = @"
    
      {
      "contributionIds": [
        "ms.vss-test-web.testcase-results-data-provider"
      ],
      "dataProviderContext": {
        "properties": {
          "testCaseId": $TestCaseID,
          "sourcePage": {
            "url": "https://dev.azure.com/{Org name}/{Project name}/_testPlans/execute?planId={Test Plan ID}&suiteId={Test Suite ID}",
            "routeId": "ms.vss-test-web.testplans-hub-refresh-route",
            "routeValues": {
              "project": "{Project name}",
              "pivots": "execute",
              "controller": "ContributedPage",
              "action": "Execute",
              "serviceHost": "$serviceHost ({Org name})"
            }
          }
        }
      }
    }
    "@
    
    #Get outcome result via run ID
    $Result = Invoke-RestMethod -Uri $Url -ContentType "application/json" -Body $Body -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST 
    
    $RunID = $Result.dataProviders."ms.vss-test-web.testcase-results-data-provider".results.runId[0]
    
    $OutComeUrl="https://dev.azure.com/{Org name}/{Project name}/_apis/test/runs/$($RunID)?api-version=6.1-preview.3"
    $OutComeResult = Invoke-RestMethod -Uri $OutComeUrl -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get
    $TestCaseResult = $OutComeResult.runStatistics.outcome
    
    Write-Host "Test case" $TestCaseID "outcome is:" $TestCaseResult
    
    
    
    
        }
    }
    

    Result:

    enter image description here