1

I use the Xunit2 selenium framework for automated test cases. Some case fails in pipeline. I want to see the attachment of the fail test case in the Test tab. How can I do this using VS test task? enter image description here

Gkm
  • 237
  • 1
  • 5
  • 19
  • Can you check the `YMAL snippet` file for `testRunner`. Check whether `#publishRunAttachments: true` and the `testResultsFiles` option should be changed to **/TEST-*.trx – KunduK Jan 25 '21 at 16:12
  • @KunduK I have enabled upload attachment in the VS test task but couldn't see publishRunAttachment in the YAML snippet. I don't know why? – Gkm Jan 26 '21 at 07:49
  • Hi @Gkm, check this [doc](https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/test/vstest?view=azure-devops), add code `publishRunAttachments: true` then kindly share the result here. – Vito Liu Jan 26 '21 at 08:05

1 Answers1

1

The option publishRunAttachments: true, it will update attachments to Test run attachment tab, check the pic below

enter image description here

After the VS test task runs, we can get the run ID through the variable VSTEST_TESTRUNID.

enter image description here

Then we could call the RESST API Create Test Result Attachment to add attachments to the test result attachment tab.

Request API:

POST https://dev.azure.com/{organization}/{project}/_apis/test/Runs/{runId}/attachments?api-version=6.0-preview.1

Request Body:

{
  "stream": "iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAIAAABvFaqvAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAABlSURBVDhP7cxBCsAgDERR739pG/CnGJI0FopQ8O2cjNP6R85QbeNQU7wT1dkijaQ3vkZoWElaoTeJojW01cYh0jwfgiFBV/lEjOZtacijN/nLkOBHhIaVDgn+Wdycp6FXzlCl9wt0Y0cAzHo/zgAAAABJRU5ErkJggg==",
  "fileName": "imageAsFileAttachment.png",
  "comment": "Test attachment upload",
  "attachmentType": "GeneralAttachment"
}

You could also check this thread

Update1

We could add task power shell and call the rest api via below script:

$connectionToken="{PAT}"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$URL = "https://dev.azure.com/{organization}/{project}/_apis/test/Runs/{runId}/attachments?api-version=6.0-preview.1" 
$body =@"
{
  "stream": "iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAIAAABvFaqvAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAABlSURBVDhP7cxBCsAgDERR739pG/CnGJI0FopQ8O2cjNP6R85QbeNQU7wT1dkijaQ3vkZoWElaoTeJojW01cYh0jwfgiFBV/lEjOZtacijN/nLkOBHhIaVDgn+Wdycp6FXzlCl9wt0Y0cAzHo/zgAAAABJRU5ErkJggg==",
  "fileName": "imageAsFileAttachment.png",
  "comment": "Test attachment upload",
  "attachmentType": "GeneralAttachment"
}
"@
$Result = Invoke-RestMethod -Uri $URL -ContentType "application/json" -Body $body -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST

Update2

Check the pic below and notice the URL

enter image description here

According to the screenshot you shared before, it seems that you want to add attachments to the test run instead of test result. So we just need test run ID and we could get the test run ID from variable after the task vs test ends.

If you want to add attachments to test result, we could list all test result via test run ID.

Sample URL:

GET https://dev.azure.com/{organization}/{project}/_apis/test/Runs/{RunID}/results

The add attachments to the test result via test result ID and test run ID.

Sample power shell script to list all test result ID

$connectionToken="{PAT}"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$URL = "https://dev.azure.com/{organization}/{project}/_apis/test/Runs/{RunID}/results"
$Result = Invoke-RestMethod -Uri $URL -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get 
 foreach($Run in $Result.value){
 Write-Host "This test run contain" $Run.id "and the test reuslt name is" $Run.testCase.name
 }

Update3

$connectionToken="{PAT}"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$URL = "https://dev.azure.com/{organization}/{project}/_apis/test/Runs/134/results"
$Result = Invoke-RestMethod -Uri $URL -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get 
#List all test result  get the test result ID via result
foreach($Run in $Result.value){

#Get the test result ID via result
If($Run.outcome -eq "Failed"){
$TestResultID = $Run.id
#Write-Host $TestResultID


#Add attachment via test run ID and test result ID
$TestResultAttachmentURL = "https://dev.azure.com/{organization}/{project}/_apis/test/Runs/{runId}/Results/$($TestResultID)/attachments?api-version=6.0-preview.1" 

$body =@"
{
  "stream": "VXNlciB0ZXh0IGNvbnRlbnQgdG8gdXBsb2FkLg==",
  "fileName": "textAsFileAttachment.txt",
  "comment": "Test attachment upload",
  "attachmentType": "GeneralAttachment"
}
"@
$TestResultAttachmentResult = Invoke-RestMethod -Uri $TestResultAttachmentURL -ContentType "application/json" -Body $body -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST
}
}

Update4

$AzureDevOpsPAT = {PAT}
$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($AzureDevOpsPAT)")) }
$UriOrga = "https://dev.azure.com/{organization}/{project}/" 
$uriAccount = $UriOrga + "_apis/test/runs?api-version=6.0"

$response = Invoke-RestMethod -Uri $uriAccount -Headers $AzureDevOpsAuthenicationHeader -Method Get
$testRunsIdSorted = $response.value | sort-object id -Descending
Write-Host "##vso[task.setvariable variable=runId]$($testRunsIdSorted[0].id | ConvertTo-Json -Depth 100)"
$result = Invoke-RestMethod -Uri https://dev.azure.com/{organization}/{project}/_apis/test/runs/$($testRunsIdSorted[0].id)/results?api-version=6.0 -Headers $AzureDevOpsAuthenicationHeader -Method Get

#List all test result  get the test result ID via result
foreach($Run in $result.value){

#Get the test result ID via result
If($Run.outcome -eq "Failed"){
$TestResultID = $Run.id
#Write-Host $TestResultID


#Add attachment via test run ID and test result ID
$TestResultAttachmentURL = "https://dev.azure.com/{organization}/{project}/_apis/test/Runs/$($runId)/Results/$($TestResultID)/attachments?api-version=6.0-preview.1" 

$body =@"
{
  "stream": "iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAIAAABvFaqvAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAABlSURBVDhP7cxBCsAgDERR739pG/CnGJI0FopQ8O2cjNP6R85QbeNQU7wT1dkijaQ3vkZoWElaoTeJojW01cYh0jwfgiFBV/lEjOZtacijN/nLkOBHhIaVDgn+Wdycp6FXzlCl9wt0Y0cAzHo/zgAAAABJRU5ErkJggg==",
  "fileName": "imageAsFileAttachment.png",
  "comment": "Test attachment upload",
  "attachmentType": "GeneralAttachment"
}
"@
$TestResultAttachmentResult = Invoke-RestMethod -Uri $TestResultAttachmentURL -ContentType "application/json" -Body $body -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST
}
}

enter image description here enter image description here

If I click on .png file, it shows nothing.

Gkm
  • 237
  • 1
  • 5
  • 19
Vito Liu
  • 7,525
  • 1
  • 8
  • 17
  • publishRunAttachments: true, I don't see VSTEST_RUNID but I can see Test run id variable. Is it the same? to call the Rest API I should add the call rest api task? – Gkm Jan 27 '21 at 09:54
  • Hi @Gkm, check the update1, we need to add task power shell and call the rest api to add attachments. You could check the VS test task log, it should show the test run ID, then compare it to the variable Test run id, it should be same. – Vito Liu Jan 27 '21 at 10:09
  • Do I need to write another script to pass runId and resultId for this script? Also, I need to add a screenshot of the screen where the test fails, from this I can do this as well right? because now I see .trx files in Add attachment tab whereas previously this failed test description was seen in Debug tab and beautifully formatted to read already. – Gkm Jan 27 '21 at 11:48
  • Hi @Gkm, Sorry for my mistake, I have updated the API and power shell script, we could add attachments to the test run via run ID, and do not need test result ID, if you want to add attachments to the test result via test result ID, we could list all test results and get the test result ID via update2 REST API, please check it and then kindly share the result here. – Vito Liu Jan 28 '21 at 07:22
  • In addition, we could add .trx files via REST API – Vito Liu Jan 28 '21 at 07:22
  • Hi @Vito Liu-MSFT, No I want to attach the screenshot against each test result. So if any test failed then I can see the screen image in Attachments tab for that test. I have VS test task in release pipeline, where I first deploy then VS Test task. Without calling the rest api I can see the trx file in Attachments tab. – Gkm Jan 29 '21 at 07:32
  • 1
    Hi @Gkm, I have added the power shell script to list all test result ID, and then we could add attachments via the test run ID and test Result ID. Could you try it and then share the result here? – Vito Liu Jan 29 '21 at 09:04
  • hey, can you please update the script on how can I loop against all test result id and add an attachment to only failing test cases. – Gkm Feb 01 '21 at 07:15
  • Hi @Gkm, Check the update3, the test run ID, we could use Test run id variable – Vito Liu Feb 01 '21 at 07:44
  • 1
    Hi @Vito Liu-MSFT, can you see update4. I have added how I am getting the test run id. – Gkm Feb 01 '21 at 08:07
  • Hi @Gkm, I recommend that you get the test run id via variable. If the answer helps, would you please accept it as the answer? So it could help other community members who get the same issues and we could archive this thread. Thanks. Have a nice day. :) – Vito Liu Feb 01 '21 at 08:13
  • Thanks for your help. these tasks are to upload the screenshot right, the image should capture in the code itself. I am capturing the image via selenium in my code but the respective image is not showing in the attachment tab. – Gkm Feb 01 '21 at 08:38
  • Do you mind sharing some screenshot of the result here? – Vito Liu Feb 01 '21 at 09:08
  • Hi, You could edit your issue description to add pic – Vito Liu Feb 01 '21 at 09:14