1
votes

I'm using VS Test task for automated testing as part of my release pipeline in Azure DevOps. In build pipeline, there is option to create a bug if whole build fails as part of definition. I do not see that in release definition.

But i'm curious to find, if i have 10 test cases in a test suite and 2 test cases got failed and 8 test cases got passed. So is there any way to create a work item (bug) for only each of the failed test cases as part of release pipeline.

Please suggest me if that option is available, even with the powershell/REST API calls to get the failed test cases and create bugs for each case.

2

2 Answers

3
votes
  1. Step: Read out the runs of your buildpipeline to get the run id - link

Url:

GET http://{instance}/{collection}/{project}/_apis/test/runs?api-version=5.0
  1. Step: Read out the results of a specific run - link

Url:

GET https://{instance}/{collection}/{project}/_apis/test/Runs/{runId}/results/{testCaseResultId}?api-version=5.0
  1. Step: analyse the response

from step 2 you will get a Json respone with the struct:

  "startedDate": "2019-07-26T04:42:59.097Z",
  "completedDate": "2019-07-26T04:42:59.107Z",
  "durationInMs": 10,
  "outcome": "Passed",
  "revision": 1,
  "state": "Completed",
  "testCase": {
    "name": "TestConstructorDescriptor"
  },
  "startedDate": "2019-07-31T09:07:51.153Z",
  "completedDate": "2019-07-31T09:07:51.153Z",
  "outcome": "Failed",
  "revision": 1,
  "state": "Completed",
  "testCase": {
    "name": "TestCreateTelegrams"
  },
  1. Step: Filter tests with Failed outcome and store the meta information in an array
  2. Step: Create your Workitem - link

Payload:

[
  {
    "op": "add",
    "path": "/fields/System.Title",
    "from": null,
    "value": "Sample task"
  }
]

Url:

POST https://{instance}/{collection}/{project}/_apis/wit/workitems/${type}?api-version=5.0

Wrap all these steps in a script (PowerShell) and added to your buildpipeline after VsTest Task. These snippets may help:

Authorization headers: (Personal Access Token needed)

#AUTHORIZATION HEADERS
$headers = @{
    "Authorization" = ('Basic {0}' -f [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)")))
    "If-Match"      = ""
}

REST API Invoke-RestMethod: (Edit -Method GET or POST)

$url = "<Enter url from steps 1, 2 or 5>"
$projs = Invoke-RestMethod -Uri $url -Method GET -ContentType "application/json" -Headers $headers -Verbose

Create Payload:

$json = @{ "op" = "add"; "path" = "$path"; ... } | ConvertTo-Json

Get ETag: (may needed for PUT or POST methods, execute before Invoke-RestMethod)

$request = Invoke-WebRequest -Uri $url -Method GET -ContentType "application/json" -Headers $headers -Verbose
$headers.'If-Match' = $request.Headers.ETag

Hope this helps.

1
votes

Yes, you can create a powershell script which will

  1. Get the list of projects.
  2. Get the list of Test Run based on your project.
  3. Get the last run object from test run list.
  4. Get the last run ID & name.
  5. Get passed/failed test cases from your last run
  6. Create a bug for failed test case

Click [here] https://github.com/ppardesi/createABug for your reference to create a bug in VSO from powershell with the help of Rest API.

Then edit your pipeline with:

  1. Create a powershell task in your pipeline after publish test result task in your pipeline.
  2. You can configure your powershell script by file or inline method.
  3. Set your task Control options - Run this task to "Even if a previous task has failed, unless the build was canceled".
  4. Also enable your agent to "allow scripts to access the OAuth token"