将结果文件添加到AssemblyCleanup中的TestContext

问题描述 投票:1回答:1

我正在尝试将文件添加到测试运行中以显示在TFS中,而不是将它们添加到单个测试中。仅将文件添加到上一次测试也是一种选择。

通过将MSTest的TestContext存储在静态变量中,我可以在我的测试类的AssemblyCleanup方法中访问它,并使用AddResultFile()来添加我的文件。但是,这些文件不会在TFS的Web UI中显示为测试运行的附件,也不会显示为上次测试的附件。

有什么方法可以在testrun中附加一次文件,或者将它们添加到最后一个测试中,或者将它们附加到测试运行中?

c# visual-studio visual-studio-2015 mstest tfs2015
1个回答
1
投票

使用TFS REST API将是一个不错的选择,您可以轻松地为测试运行或测试结果添加附件。

将文件附加到测试运行:

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 }
}

将文件附加到测试结果:

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 }
}

您可以使用以下代码获取文件的“stream”字符串:

Byte[] bytes = File.ReadAllBytes("path");
String file = Convert.ToBase64String(bytes);
© www.soinside.com 2019 - 2024. All rights reserved.