VSTS - Post 从构建到 API 端点的测试结果

VSTS - Post test results from a build to an API end point

我需要 post 测试结果(例如测试数量 passed/failed)报告 api。我想在 xml 中发布结果并添加一个 powershell 步骤来读取和提取 xml 文件中的信息并向 api 发出 post 请求。

有没有人试过这种方法或者有没有更好的方法来实现同样的事情?

谢谢!

如果您愿意托管其他服务,您可以将测试结果发布为构建工件并通过 VSTS API 访问它,然后对其进行解析,然后 post 将其发送到您的目的地APIhttps://docs.microsoft.com/en-us/rest/api/vsts/build/artifacts/get?view=vsts-rest-4.1

GET https://{accountName}.visualstudio.com/{project}/_apis/build/builds/{buildId}/artifacts?artifactName={artifactName}&api-version=4.1

如果您决定使用 powershell 解析构建任务中的测试结果,您可以使用构建任务对目标 API 端点进行 API 调用。

这就是我最终所做的。

我不需要任务 'Publish Test Results',因为 'Test Assemblies' 任务已经完成了。

测试结果以 .trx 文件的形式发布,因为我使用 xUnit 作为测试框架。

我的 powershell 脚本看起来像这样。

[Reflection.Assembly]::LoadFile("$Env:BUILD_SOURCESDIRECTORY\<ProjectFolder>\Scripts\Newtonsoft.Json.dll")

$resultFiles = Get-ChildItem -Path ".\TestResults\*.trx"      

foreach ($resultFile in $resultFiles) {
    Write-Output $resultFile

    $data = [xml](Get-Content $resultFile)    
    $json = [Newtonsoft.Json.JsonConvert]::SerializeXmlNode($data)
    $json = $json -replace '"@', '"'

    Invoke-WebRequest -Uri "https://my-azure-function/api/VstsWebhook?buildId=$Env:BUILD_BUILDID&buildNumber=$Env:BUILD_BUILDNUMBER&code=<secret>" -Method POST -ContentType 'application/json' -Body $json
}

希望这对处于相同情况的其他人有所帮助。