如何在 Pytest JUNIT xml 上附加屏幕截图以显示在 Azure DevOps 上?

How to attach screenshots at Pytest JUNIT xml to appear on Azure DevOps?

如何使用 Pytest 在 JUNIT xml 中自动添加屏幕截图?我想在报告中附上测试执行过程中的截图,但是这部分的Pytest文档不是很清楚

另外,Azure DevOps documentation 说它可以自动上传所有生成的文件作为附件。这是否取代了手动附加文件的需要?它是如何附加文件A和测试X的?

提前致谢!

I want the screenshots taken during the execution of tests to be attached to the report

您可以尝试调用 Create Test Result Attachment rest api 将屏幕截图附加到测试附件选项卡。

powershell 任务中的示例脚本:

$file= [IO.File]::ReadAllBytes("filepath$Name.png")
$Base64file= [Convert]::ToBase64String($file)
echo $Base64file

$token = "PAT"

$url="https://dev.azure.com/{org}/{proj}/_apis/test/Runs/$(runId)/results/$($TestResultID)/attachments?api-version=6.0-preview.1"

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

$JSON = "
    {
       `"stream`": `"$Base64file`",
       `"fileName`": `"$Name.png`",
       `"comment`": `"Test attachment upload`",
       `"attachmentType`": `"GeneralAttachment`"
    }"
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json

这篇给出了详细的指引,大家可以参考一下

此外,您可以直接尝试使用扩展程序 Publish test result screenshot 中的 Publish screenshots for test failure 任务。

此扩展提供了一个构建任务以集成到您的持续集成管道中。此任务将从 Visual Studio 测试 (TRX) 以外的来源(例如,来自 jUnit)上传屏幕截图到测试结果报告。