testingunit testing数据文件的path

我目前在VS 2008中使用标准的Microsoftunit testing套件.ReSharper 4.5也被安装。 我的unit testing依赖于预加载数据文件的TestInitialize方法。 这个testing数据文件的path将会有所不同,具体取决于我是否使用标准的Ctrl-R + Ctrl-T命令与Resharperunit testing执行命令在VS 2008中运行unit testing。

我的TestInitialize方法如何知道unit testing数据文件的正确path?

更新:

testing数据足够大,我不想把它推入一个string,所以宁愿保留它作为外部文件。 我的testing项目的文件结构是使用MVC应用程序创build的标准unit testing项目的文件结构。 在testing项目的根目录下,创build了一个名为“testing数据”的新文件夹。 这是我想要访问这个文件夹,无论testing运行。

你说testing文件的位置将根据testing运行器而有所不同,所以我认为它包含在项目中并与dll一起复制。

string path = AppDomain.CurrentDomain.BaseDirectory; 

这将使您获得执行testing的文件夹。

[编辑]

在Visual Studio中。

resharper – >选项 – >工具 – >unit testing – >从指定文件夹运行结果(或者更改testing项目的项目输出文件夹)

您可以在哪里指定testing数据的文件夹,或相对于指定的文件夹。

(原来的答案更新为也接受.net核心多目标项目输出path)

它假设您的testing数据文件位于根“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); } 

你能不能在你的testing套件初始化程序中从头开始创buildtesting数据? 这不会依靠它在一个特定的位置,它可以避免无意的篡改。