0

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

4

1 回答 1

1

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

在此处输入图像描述

VS测试任务运行后,我们可以通过变量获取运行ID VSTEST_TESTRUNID

在此处输入图像描述

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

请求接口:

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

你也可以检查这个线程

更新1

我们可以添加任务电源外壳并通过以下脚本调用其余 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,我们可以在任务与测试结束后从变量中获取测试运行 ID。

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

示例网址:

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

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

用于列出所有测试结果 ID 的示例 power shell 脚本

$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 文件,它不会显示任何内容。

于 2021-01-26T09:20:30.573 回答