问题
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