I want the screenshots taken during the execution of tests to be
attached to the report
You can try to call the Create Test Result Attachment rest api to attach screenshots to the test Attachment tab.
Sample script in powershell task:
$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
This ticket gives detailed guidelines, you can refer to it for details.
In addition, you could directly try to use the Publish screenshots for test failure
task from the extension Publish test result screenshot.
This extension provides a Build task to integrate in your continuous integration pipeline. This task will upload screenshots to test result reports from sources other than Visual Studio Test (TRX), for instance, from jUnit.