c# - TestContext 定义的目录的目的是什么?

标签 c# .net unit-testing testing testcontext

这些是在 TestContext 类和 their respective definitions 中定义的(未弃用的)目录.

部署目录

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

结果目录

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

测试结果目录

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

测试运行目录

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

测试运行结果目录

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

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

最佳答案

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

(我相信第二个来源有一个错误,其中 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 中设置设置。

[...] 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 中进行单元测试时使用这些文件夹中的任何一个。但是,在进行远程测试和收集诊断数据时,使用此文件夹可能很重要。


我一直在想InOut我觉得令人困惑的名字。但是,如果您假设测试 Controller 的视角,那么将测试部署到 Out 是有意义的。 (测试 Controller 输出),当测试完成时,结果从 In 中收集。文件夹(测试 Controller 输入)。当然,这纯粹是我的猜测。

关于c# - TestContext 定义的目录的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36548205/

相关文章:

c# - 打开图像并使用相同的参数重新保存,c#

c# - WCF 服务能否同时支持缓冲和流式传输模式?

c# - Silverlight/.net 的条码阅读器组件、资源、推荐、动物之森?天啊

java - 在 .Net 和 Java 之间共享数据(非对象)的最快(性能方面)方式

.net - 从数据库中显示用户数量的最快方法?

php - Cakephp 测试数据库 - 模型问题

python - 如何以一种可以在单个命令中运行所有测试的方式组织 python 测试?

c# - 当前 OperationContext 在 WCF Windows 服务中为 null

c# - Frame.Navigate to a XAML Page in different assembly [WinRT]

python - 为什么我收到 KeyError : "Django settings doesn' t define RESOLVER"?