这些是在TestContext类和their respective definitions中定义的(不建议使用的)目录。

DeploymentDirectory


获取为测试运行部署的文件的目录。此属性通常包含TestRunDirectory的子目录。


ResultsDirectory


获取包含测试结果和用于测试运行的测试结果目录的顶级目录。这通常是TestRunDirectory的子目录。


TestResultsDirectory


获取测试结果文件的目录。


TestRunDirectory


获取包含已部署文件和结果文件的测试运行的顶级目录。


TestRunResultsDirectory


获取测试运行结果文件的顶级目录。此属性通常包含ResultsDirectory的子目录。


我发现它们很模棱两可。每个目录都有一些可靠的示例用法吗?例如。如果我要测试文件I / O,如果我想创建一个临时的lorem ipsum文件,可以使用其中的任何一个吗?

最佳答案

为了回答您的问题,我从几个来源收集了信息:


What’s in the TestResults directory?中的How to: Deploy Files for Tests on MSDN
TestContext folders explained由Bhuvaneshwari K [MSFT]撰写


(我相信第二个来源在TestRunResultsDirectoryTestResultsDirectory的位置互换时有一个错误。)

当Visual Studio执行测试时,将创建几个文件夹。

基本文件夹

使用以下模板来命名测试基础文件夹:

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>


要实际使用此.runsettings文件,您将必须在Visual Studio 2015中使用以下菜单选项:测试>测试设置>选择测试设置文件。如果使用的是ReSharper单元测试运行程序,则在“ ReSharper选项”对话框的“工具”>“单元测试”>“ MsTest”中进行设置。


  [...]如果我要测试文件I / O,如果我想创建一个临时的lorem ipsum文件,这些方法都可以吗?


是的,您应该对临时文件使用TestRunResultsDirectory。实际上,我认为在Visual Studio中进行单元测试时,您应该能够使用这些文件夹中的任何一个。但是,在进行远程测试和收集诊断数据时,使用此文件夹可能很重要。



我一直在思考InOut的名称,这些名称令我感到困惑。但是,如果您以测试控制器为视角,则将测试部署到Out(测试控制器输出)是有意义的,并且当测试完成时,会从In文件夹(测试控制器输入)中收集结果。当然,这纯粹是我的猜测。

10-06 11:07
查看更多