【问题标题】:Adding result files to TestContext in AssemblyCleanup在 AssemblyCleanup 中将结果文件添加到 TestContext
【发布时间】:2018-02-28 15:31:16
【问题描述】:
我正在尝试将文件添加到要在 TFS 中显示的测试运行中,而不是将它们添加到单个测试中。仅将文件添加到最后一个测试也是一种选择。
通过将 MSTest 的 TestContext 存储在静态变量中,我可以在我的测试类的 AssemblyCleanup 方法中访问它,并使用 AddResultFile() 添加我的文件。但是,这些文件不会在 TFS 的 Web UI 中显示为测试运行的附件,也不会显示为上次测试的附件。
有什么方法可以在测试运行中附加文件一次,要么将它们添加到最后一个测试,要么将它们附加到测试运行?
【问题讨论】:
标签:
c#
visual-studio
visual-studio-2015
mstest
tfs-2015
【解决方案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 }
}
您可以使用以下代码获取文件的“流”字符串:
Byte[] bytes = File.ReadAllBytes("path");
String file = Convert.ToBase64String(bytes);