Azure DevOps:将文件附加到测试结果/从 YAML 管道运行
我有一些(剧作家)自动化测试,通过 YAML 管道在 Azure DevOps 中运行。
我启用了 Playwright 的跟踪功能,因此每个测试都会生成一个包含跟踪的 zip 文件。我的目标是让失败的测试将跟踪 zip 文件附加到测试结果中,以便我们可以轻松地单击并查看到底出了什么问题。这些文件最大可达 15MB 左右。
这是我希望它们出现在 Azure DevOps 中的位置:
查看 文档 我看到有一个用于发布附件的 REST API,但我不清楚如何从 YAML 管道调用此 API。即对 API 进行身份验证,获取测试用例 ID 和以 base64 形式发送 zip 文件。
我想我正在寻找执行此操作的示例 YAML 任务。
I have some (Playwright) automated tests that run in Azure DevOps via a YAML pipeline.
I have enabled the trace feature of Playwright so each test produces a zip file containing the trace. My goal is for failing tests to have the trace zip file attached to the test result, allowing us to easily click through and see exactly what went wrong. These files can be up to about 15MB.
This is where I want them to appear in Azure DevOps:
Looking at the documentation I see there is a REST API to post attachments, but I'm unclear how to invoke this API from the YAML pipeline. i.e. authenticating for the API, getting the test case ID & sending the zip file as base64.
I guess I'm looking for an example YAML task that does this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:
嗨,对之前的回复进行了一些更新,通过拦截网络流量,我找到了比 REST API 更简单的方法。首先,请参考这个文档,通过预定义的变量Build.BuildId可以轻松获取当前pipeline的buildid。然后,请通过 get 方法运行此请求以获取测试结果(基于您的构建管道运行):
https://vstmr.dev.azure.com///_apis/testresults /resultsbypipeline?pipelineId=
此方法没有官方文档或参考,但它有效并且我已经测试过。
原始答案:
您可以使用您想要发送请求的任何语言/脚本语言。
对于身份验证,您可以使用 PAT 令牌,以下是详细步骤:
https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=Windows
例如,如果您使用Python,
这是通过base64(Python)编写zip文件的方法:
在 Python 中对 zip 文件进行 Base64 编码
然后使用下面的代码上传 zip文件。
对于测试用例 ID,您可以在工作项 url 中获取它:
或者您可以使用此 REST API 列出测试套件中的测试用例:
https://learn.microsoft.com/en-us/rest/api/azure/devops/test/test-suites/get-test-cases?view=azure-devops-rest-5.0
YAML 管道应该是这样的:
就简单了,具体怎么做就需要你自己设计了。
Update:
Hi, some update on the previous reply, by intercepting network traffic, I found an easier way than REST API. First of all, please refer to this document, the buildid of the current pipeline can be easily obtained through the predefined variable Build.BuildId. Then, please run this request by get method to get test results(Based on your build pipeline run):
https://vstmr.dev.azure.com/<orgname>/<projectname>/_apis/testresults/resultsbypipeline?pipelineId=<buildid>
There is no official documentation or reference for this method, but it works and I have tested it.
Original Answer:
You can use any language/script language you want to send the request.
For the authentication, you can use the PAT token, here are detailed steps:
https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=Windows
For example, if you use Python,
This is how to write zip file via base64(Python):
base64 encode a zip file in Python
And then use below code to upload the zip file.
For test case id, you can get it in the workitem url:
Or you can use this REST API to list the test cases in test suits:
https://learn.microsoft.com/en-us/rest/api/azure/devops/test/test-suites/get-test-cases?view=azure-devops-rest-5.0
YAML pipeline should be like this:
Just a simple, for how to do it on your side, you need to design it by yourself.