这些是在 TestContext
类和 their respective definitions 中定义的(未弃用的)目录.
部署目录
- 获取为测试运行部署的文件的目录。此属性通常包含 TestRunDirectory 的子目录。
结果目录
- 获取包含测试运行的测试结果和测试结果目录的顶级目录。这通常是 TestRunDirectory 的子目录。
测试结果目录
- 获取测试结果文件的目录。
测试运行目录
- 获取包含已部署文件和结果文件的测试运行的顶级目录。
测试运行结果目录
- 获取测试运行结果文件的顶级目录。此属性通常包含 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] 撰写
(我相信第二个来源有一个错误,其中 TestRunResultsDirectory
和 TestResultsDirectory
的位置被交换了。)
当 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 中设置设置。
[...] if I were to test file I/O would any of those be OK if I wanted to create a temporary lorem ipsum file?
是的,你应该使用 TestRunResultsDirectory
对于临时文件。实际上,我认为您应该能够在 Visual Studio 中进行单元测试时使用这些文件夹中的任何一个。但是,在进行远程测试和收集诊断数据时,使用此文件夹可能很重要。
我一直在想In
和 Out
我觉得令人困惑的名字。但是,如果您假设测试 Controller 的视角,那么将测试部署到 Out
是有意义的。 (测试 Controller 输出),当测试完成时,结果从 In
中收集。文件夹(测试 Controller 输入)。当然,这纯粹是我的猜测。
关于c# - TestContext 定义的目录的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36548205/