Search code examples
c#.netunit-testingtestingtestcontext

What is the purpose of directories defined by TestContext?


These are the (non-deprecated) directories defined in TestContext class and their respective definitions.

DeploymentDirectory

  • Gets the directory for files deployed for the test run. This property typically contains a subdirectory of TestRunDirectory.

ResultsDirectory

  • Gets the top-level directory that contains test results and test result directories for the test run. This is typically a subdirectory of TestRunDirectory.

TestResultsDirectory

  • Gets the directory for the test result files.

TestRunDirectory

  • Gets the top-level directory for the test run that contains deployed files and result files.

TestRunResultsDirectory

  • Gets the top-level directory for the test run result files. This property typically contains a subdirectory of ResultsDirectory.

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?


Solution

  • 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.