7

I am quite new to visual studio. I am having visual studio community 2015.

What I would like to do is when outputting specific data in my unittest with MSTest, I would like to save the output into a file.

I have seen that at each running test, a folder is generated automatically in TestResuls/Deploy_username date hour/out. I am looking for a way to save the tests output in that folder.

Is this possible or is there other ways to copy the files or report my data? I have looked at these following links: https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.deploymentitemattribute.aspx

https://msdn.microsoft.com/en-us/library/ee256991.aspx

DeploymentItem not deploying files

Generating Unit Test Reports in Visual Studio 2013

None of these links helped me in achieving what I wanted.

Thank you in advance for your help.

Community
  • 1
  • 1
tuttifolies
  • 1,417
  • 2
  • 14
  • 20

1 Answers1

9

I will not explain how File Output is handled in .Net, I presume you have this Knowledge.

If you have a TestClass declared with Attribute [TestClass] you can add a public Property TestContext of Type TestContext (see MSDN), which will get set by MSTest while executing the Test. There you have the Properties TestResultsDirectory (which typically leads to the "IN" Folder) and TestDeploymentDir (which typically leads to the "OUT" Folder) . e.g.: TestContext.TestResultsDirectory would lead to "D:\Visual Studio 2015\Projects\UnitTestProject1\TestResults\Deploy_user 2017-02-23 07_37_49\In"

If you need the TestContext earlier then in the [TestMethod] you need to use a public static void TestInit(TestContext testContext) Method in your TestClass, so you get the Instance of the TestContext when your Class got created.

There are also Properties like TestLogsDir, TestDir which are deprecated, you can read in the MSDN docs about it.

Sebastian
  • 2,874
  • 25
  • 31
  • 1
    Thanks for the information. I just want to point out that as of MSTest.TestFramework.1.3.2, the property `TestDeploymentDir` mentions this in the summary: `Same as Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.DeploymentDirectory. Use that property instead.` – Tipx Jun 16 '20 at 15:54