C# 单元测试的测试数据文件的路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1776331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:43:03  来源:igfitidea点击:

Path to Test Data Files for Unit Testing

c#unit-testingresharper

提问by BigBrother

I am currently using the standard Microsoft Unit Test suite in VS 2008. ReSharper 4.5 is also installed. My unit tests rely on an TestInitialize method which pre-loads a data file. The path to this test data file will differ depending on if I run the unit test from within VS 2008 using the standard Ctrl-R + Ctrl-T command versus the Resharper unit test execution command.

我目前在 VS 2008 中使用标准的 Microsoft 单元测试套件。还安装了 ReSharper 4.5。我的单元测试依赖于预加载数据文件的 TestInitialize 方法。此测试数据文件的路径将根据我是使用标准 Ctrl-R + Ctrl-T 命令还是 Resharper 单元测试执行命令从 VS 2008 中运行单元测试而有所不同。

How can my TestInitialize method know the correct path to the unit test data files?

我的 TestInitialize 方法如何知道单元测试数据文件的正确路径?

Update:

更新:

The test data is sizable enough that I don't want to push it into a string so prefer to keep it as an external file. The file structure of my test project is that of the standard unit test project created with an MVC application. Under the root of the test project, a new folder was created called 'Test Data'. It's this folder I'd like to access regardless of test runner.

测试数据足够大,我不想将其推送到字符串中,因此更愿意将其保留为外部文件。我的测试项目的文件结构是使用 MVC 应用程序创建的标准单元测试项目的文件结构。在测试项目的根目录下,创建了一个名为“Test Data”的新文件夹。无论测试运行程序如何,我都想访问这个文件夹。

回答by ChrisF

I would create the test data from scratch in the test suite initialisation routine.

我会在测试套件初始化例程中从头开始创建测试数据。

This would mean that it wasn't reliant on it being at a specific location and it would also be safe from inadvertent tampering.

这意味着它不依赖于它位于特定位置,并且也不会被无意篡改。

回答by Mikael Svenson

You're saying the test file's location will differ depending on the test runner, so I assume it's included in the project and copied together with the dll's.

您是说测试文件的位置会因测试运行程序而异,所以我假设它包含在项目中并与 dll 一起复制。

string path = AppDomain.CurrentDomain.BaseDirectory;

This will get you the folder where you're executing the test from.

这将为您提供执行测试的文件夹。

[Edit]

[编辑]

In Visual Studio.

在 Visual Studio 中。

Resharper -> Options -> Tools -> Unit Testing -> Run Results from: Specified Folder (or change the project output folder of your test project)

Resharper -> 选项 -> 工具 -> 单元测试 -> 运行结果来自:指定文件夹(或更改测试项目的项目输出文件夹)

Where you can specify the folder of your test data, or relative to the specified folder.

您可以在其中指定测试数据的文件夹,或相对于指定的文件夹。

回答by DaniCE

(original answer updated to also accept .net core multi-target project output paths)

(原始答案更新为也接受 .net core 多目标项目输出路径)

It assumes your test data files are in a folder that you pass as a parameter "testDataFolder" inside a root "Test_Data" folder:

它假设您的测试数据文件位于您在根“Test_Data”文件夹中作为参数“testDataFolder”传递的文件夹中:

public static string GetTestDataFolder(string testDataFolder)
{
    string startupPath = ApplicationEnvironment.ApplicationBasePath;
    var pathItems = startupPath.Split(Path.DirectorySeparatorChar);
    var pos = pathItems.Reverse().ToList().FindIndex(x => string.Equals("bin", x));
    string projectPath = String.Join(Path.DirectorySeparatorChar.ToString(), pathItems.Take(pathItems.Length - pos - 1));
    return Path.Combine(projectPath, "Test_Data", testDataFolder);
}