Adding result files to TestContext in AssemblyCleanup

亡梦爱人 提交于 2019-12-11 08:13:52

问题


I'm trying to add files to a test run to be shown in TFS, as opposed to adding them to an individual test. Adding files only to the last test would also be an option.

By storing the TestContext of MSTest in a static variable, I can access it in the AssemblyCleanup method of my test class and use AddResultFile() to add my files. However, the files do not appear as attachment of the test run in TFS' web UI, and also do not appear as attachment of the last test.

Any way to attach files once in a testrun, either by adding them to the last test or attaching them to the test run?


回答1:


Use TFS REST API will be a good option, you can add an attachment to a test run or test result easily.

Attach a file to a test run:

POST https://{instance}/DefaultCollection/{project}/_apis/test/runs/{run}/attachments?api-version={version}

Content-Type: application/json

{
  "stream": { string },
  "fileName": { string },
  "comment": { string },
  "attachmentType": { string }
}

Attach a file to a test result:

POST https://{instance}/DefaultCollection/{project}/_apis/test/runs/{run}/results/{result}/attachments?api-version={version}

Content-Type: application/json

{
  "stream": { string },
  "fileName": { string },
  "comment": { string },
  "attachmentType": { string }
}

You can use the following code to get the "stream" string for a file:

Byte[] bytes = File.ReadAllBytes("path");
String file = Convert.ToBase64String(bytes);


来源:https://stackoverflow.com/questions/49033133/adding-result-files-to-testcontext-in-assemblycleanup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!