These are the (non-deprecated) directories defined in TestContext
class and their respective definitions.
DeploymentDirectory
ResultsDirectory
TestResultsDirectory
TestRunDirectory
TestRunResultsDirectory
I find them quite ambiguous. Is there some solid example usage for each directory? E.g. if I were to test file I/O would any of those be OK if I wanted to create a temporary lorem ipsum file?
To answer your question I have gathered information from a few sources:
(I believe that the second source has a mistake where the location of the TestRunResultsDirectory
and TestResultsDirectory
are swapped.)
When Visual Studio executes tests several folders are created.
Base folder
The test base folder is named using this template:
TestResults\Deploy_<user name> <timestamp>
If you specify setup and cleanup scripts in a .testsettings
file, this folder contains those scripts. The .testsettings
file also allows you to change the name of the folder.
Out folder
The base folder contains a folder named Out
. The Out
folder is the actual deployment folder, to which assemblies and other deployment files are copied when the test run starts.
If you need to refer to any of your deployed files, you should be using this folder.
In folder
Code-coverage results and certain other test results are stored in the folder named In
located in the base folder.
If you add a file to the test result using the TestContext.AddResult()
method, the file should be stored in this folder. (I haven't verified this claim myself as test results are stored when using Microsoft Test Manager and during TFS build; not when running tests in Visual Studio.)
In\<machine name> folder
A machine specific folder is created as a subfolder of the In
folder. If you need to create temp files during the test run, you should be using this folder.
Here is a table that explains how TestContext
properties map to the folders described above:
Property | Value ---------------------------------------+--------------------------- TestRunDirectory | Base folder DeploymentDirectory | Out folder ResultsDirectory, TestResultsDirectory | In folder TestRunResultsDirectory | Machine specific In folder
If all tests are successful then the folders are deleted by default. To change this behavior you can add a .runsettings
file to your solution with the following contents:
<RunSettings>
<MSTest>
<DeleteDeploymentDirectoryAfterTestRunIsComplete>False</DeleteDeploymentDirectoryAfterTestRunIsComplete>
</MSTest>
</RunSettings>
To actually use this .runsettings
file you will have to use the following menu selection in Visual Studio 2015: Test > Test Settings > Select Test Settings File. If you are using the ReSharper unit test runner then the settings are set in Tools > Unit Testing > MsTest in the ReSharper Options dialog box.
[...] if I were to test file I/O would any of those be OK if I wanted to create a temporary lorem ipsum file?
Yes, you should use TestRunResultsDirectory
for temporary files. Actually, I think you should be able to use any of these folders when unit testing inside Visual Studio. However, when doing remote tests and collecting diagnostics data it may be important to use this folder.
I have been thinking about the In
and Out
names which I find confusing. However, if you assume the perspective of a test controller it makes sense that tests are deployed to Out
(test controller output) and when the tests are complete the results are collected from the In
folder (test controller input). This is pure speculation on my part, of course.