如何使用Azure DevOps REST API将文件附加到Visual Studio Test? [英] How to attach files to Visual Studio Test with Azure DevOps REST API?

查看:69
本文介绍了如何使用Azure DevOps REST API将文件附加到Visual Studio Test?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR:如何将发布与通过API在测试"标签中可见的测试相关联?

我正在运行一个发布管道,该管道针对正在部署的网站执行测试.测试会生成文件(在我的测试代码内),并且我想使用DevOps REST API将这些文件附加到与发行版关联的测试报告中.

注意:作为发行管道的一部分,我在发行后将这些测试作为冒烟测试运行,以便可以测试已部署的网站.作为构建管道的一部分,这是不可能的.

我可以

其他:此SO帖子中的链接似乎已更改,并且不再指向目标页面,但现在指向REST API的整体文档.

目前,我认为该流程应为:

  • 获取发布详细信息
  • 获取测试运行ID
  • 获取测试用例结果ID
  • 创建测试结果附件

主要问题是从发布信息中获取测试运行ID 测试用例结果ID .

解决方案

我想我对此有些迟了,但仍然如此.

您对这个过程是正确的.通过访问发行版中相应任务的日志,可以获得测试运行ID .这是所需的端点:

  https://{organizationName} .vsrm.visualstudio.com/{projectName}/_apis/release/releases/{releaseID}/environments/{stageID}/deployPhases/{deployPhaseID}/tasks/{taskId}/日志 

请注意,测试运行ID和测试ID仅在测试运行任务完成后可用.

我建议您通过访问由azure devops提供的默认变量(例如 $(Release.ReleaseId),直接从版本中获取版本ID,阶段ID和部署阶段ID,其他变量可以轻松地用Google搜索)),而不是通过API进行提取并按名称进行匹配,因为这样做会花费时间,并且不会返回所有版本,只会返回第一个版本或第100个版本.

此后,您需要获取 TaskID .我发现按任务名称进行查找是可行且适用的.只需通过API通过ID来获取发布,然后使用此端点查找与所需名称匹配的任务即可:

  https://{organizationName} .vsrm.visualstudio.com/{projectName}/_apis/release/releases/{releaseId}?api-version = 5.0 

遍历所有的deploySteps,deployPhases,deployJobs来获取任务名称比较麻烦,但是可能(我使用LINQ).

获得这4个基本ID之后,您现在可以获取运行测试"任务的日志.在收到的日志中,我使用了正则表达式来提取测试运行ID.然后,有了测试运行ID,您可以获取测试运行结果的列表:

  https://{organizationName} .visualstudio.com/{projectName}/_apis/test/runs/{testRunId}/结果 

要将某些文件附加到测试中,您将需要此测试的AzDo ID.你怎么能得到它?好吧,我可以考虑按测试名称映射测试(您需要事先存储这些测试的列表),然后循环访问获取的测试结果和存储的列表.找到匹配项后,获取相应的ID,然后附加您想要的任何内容:

  https://{organizationName} .visualstudio.com/{projectName}/_apistest/Runs/{testRunId}/Results/{testId}/attachments?api-version=6.1-preview.1 

TL;DR: How do I relate a release to the tests visible in the Tests tab via the API?

I am running a Release Pipeline which executes tests against the website being deployed. The tests generate files (within my test code) and I want to use the DevOps REST API to attach those files to the Test report associated with the Release.

Note: I am running these tests as smoke tests after release as part of the release pipeline so that I can test the deployed website. This would not be possible as part of the build pipeline.

I can get the release information, but I'm having trouble identifying how to find the related test(s) and attach a file.

Additional: The link in this SO post appears to have been changed and no longer points to the intended page, but now points to the overall documentation for the REST API.

Currently, I believe the process should be:

  • Get release details
  • Get test run id
  • Get test case result id
  • Create test result attachment

The primary problem is getting the test run id and test case result id from the release information.

解决方案

I guess I'm a bit late with this, but still.

You are right about the process. Getting Test run ID is possible via accessing logs of corresponding task inside the release. Here's the needed endpoint:

 https://{organizationName}.vsrm.visualstudio.com/{projectName}/_apis/release/releases/{releaseID}/environments/{stageID}/deployPhases/{deployPhaseID}/tasks/{taskId}/logs

Please note that test run ID and test IDs will only be available after the test run task is finished.

I'd recommend getting release ID, stage ID and deployment phase ID directly from the release by accessing default variables provided by azure devops (e.g. $(Release.ReleaseId), other ones are easily googled) rather than fetching via API and matching by name, cause it takes time and does not return all releases, only 100 first or smth like that.

After this, you need to get TaskID. I found it possible and applicable looking for that by Task Name. Just fetch the release by it's ID via API and look for task which matches needed name using this endpoint:

https://{organizationName}.vsrm.visualstudio.com/{projectName}/_apis/release/releases/{releaseId}?api-version=5.0

Going through all the deploySteps, deployPhases, deployJobs to get the task name is a bit cumbersome, but possible (I used LINQ).

After getting these 4 base IDs, you are now able to get logs of the Run tests task. In received log I have used a regex to extract test run ID. Then, having test run ID, you can grab the list of test run results:

https://{organizationName}.visualstudio.com/{projectName}/_apis/test/runs/{testRunId}/results

To attach some files to a test, you will need this test's AzDo ID. How can you get it? Well, I can think of mapping the tests by their names (you will need to store a list of those beforehand) and then looping through fetched test results and the stored list. After a match is found, get the respective id, and attach anything you want:

https://{organizationName}.visualstudio.com/{projectName}/_apistest/Runs/{testRunId}/Results/{testId}/attachments?api-version=6.1-preview.1

这篇关于如何使用Azure DevOps REST API将文件附加到Visual Studio Test?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆