Azure Devops运行结果步骤和运行摘要详细信息附件API

oug3syen  于 2023-05-23  发布在  其他
关注(0)|答案(1)|浏览(132)

我试图获取测试运行结果步骤和测试运行结果摘要详细信息的附件,但无法找到与我在下面附加的图像相关的任何API矩形1是测试运行结果步骤的附件,矩形2是测试运行结果摘要详细信息的附件

如果有人对这些特殊的API有任何了解,请让我知道。
我已经检查了AZURE API Documentation,但找不到具体的API,如果我有missied的东西,请让我知道。谢谢

laik7k3q

laik7k3q1#

通过调用Get Test Result Attachments REST API,我们可以获取所有附件的ID:

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

之后,如果你想获取附件,你可以用特定的Attachment ID调用Attachments - Get Test Result Attachment Zip REST API。

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

请注意,REST API Attachments - Get Test Result Attachment Zip将显示附件的上下文,而不是直接下载附件。如果您想下载附件,可以编写一个脚本将它们保存到本地目录。以下PowerShell脚本供您参考:

$AttachmentsOutfile = "D:\Test\HellWorld.java"

$connectionToken="You PAT Here"

$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::  
ASCII.GetBytes(":$($connectionToken)"))

$AuditLogURL = "https://dev.azure.com/{organization}/{project}/_apis/test/Runs/{runId}/Results/{testCaseResultId}/attachments/{attachmentId}?api-version=6.0-preview.1" 

$AuditInfo = Invoke-RestMethod -Uri $AuditLogURL -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get –OutFile $AttachmentsOutfile

更新:

但是,API只能从测试运行UI中获取附件(通过单击Add attachment按钮进行附加)。
为了获取测试运行结果步骤和测试运行结果摘要的附件,我们可以调用Results - Get REST API,并添加参数detailsToInclude=iterations

GET https://dev.azure.com/{organization}/{project}/_apis/test/Runs/{runId}/results/{testCaseResultId}?detailsToInclude=iterations&api-version=6.0

然后我们可以通过ID下载附件。下面的PowerShell脚本供大家循环下载参考:

Param(
   [string]$orgurl = "https://dev.azure.com/{org}",
   [string]$project = "Test0924",
   [string]$downloadlocation = "C:\temp\1025\",
   [string]$TestRunId = "1000294",
   [string]$ResultId = "100000",
   [string]$user = "",
   [string]$token = "PAT"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

#List test result and test step attachments: 
$testresultUrl = "$orgurl/$project/_apis/test/Runs/$TestRunId/Results/$($ResultId)?detailsToInclude=iterations&api-version=6.0" 
$attachments = (Invoke-RestMethod -Uri $testresultUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}).iterationDetails.attachments

ForEach ($attachment in $attachments) {
#Get test result and step attachments:
$attachmentid = $attachment.id
$attachmentname = $attachment.name
$attachmenturl = "$orgurl/$project/_apis/test/Runs/$TestRunId/Results/$ResultId/attachments/$($attachmentid)?api-version=6.0"
Invoke-RestMethod -Uri $attachmenturl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -OutFile $downloadlocation\$attachmentname
}

相关问题