开发者

Path to Test Data Files for Unit Testing

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.

How can my Tes开发者_Go百科tInitialize method know the correct path to the unit test data files?

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.


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.

string path = AppDomain.CurrentDomain.BaseDirectory;

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

[Edit]

In Visual Studio.

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

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


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

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

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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜