Azure 管道:如何在 VS 测试任务中添加失败的 selenium Xunit 测试用例附件

Azure pipelines: How to add failed selenium Xunit test case attachment in VS test task

我将 Xunit2 selenium 框架用于自动化测试用例。有些案例在管道中失败。我想在“测试”选项卡中查看失败测试用例的附件。如何使用 VS 测试任务执行此操作?

选项 publishRunAttachments: true,它会将附件更新到测试 运行 附件选项卡,查看下面的图片

VS测试任务运行s后,我们可以通过变量VSTEST_TESTRUNID.

获取运行ID

然后我们可以调用 RESST API Create Test Result Attachment 将附件添加到 测试结果 附件选项卡。

请求API:

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

请求正文:

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

你也可以检查这个thread

更新1

我们可以添加任务 power shell 并通过以下脚本调用其余的 api:

$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

更新2

检查下图并注意 URL

根据您之前分享的屏幕截图,您似乎想将附件添加到测试 运行 而不是测试结果。所以我们只需要测试运行 ID,我们可以在任务vs测试结束后从变量中获取测试运行 ID。

如果你想在测试结果中添加附件,我们可以通过测试运行 ID列出所有测试结果。

样本URL:

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

通过测试结果ID和测试运行 ID向测试结果添加附件。

样本功率shell列出所有测试结果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
 }

更新3

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

更新4

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

如果我点击 .png 文件,它什么也没有显示。