Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having output from UnitTest in the "Out" folder in TestResults' deploymentFolder, with MSTest in Visual Studio

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.

like image 318
tuttifolies Avatar asked Aug 24 '16 08:08

tuttifolies


People also ask

How do you add MSTest to a project?

On the File menu, select New > Project, or press Ctrl+Shift+N. On the Create a new project page, type unit test into the search box. Select the project template for the test framework that you want to use, for example MSTest Test Project or NUnit Test Project, and then select Next.


1 Answers

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.

like image 109
Sebastian Avatar answered Oct 01 '22 00:10

Sebastian