Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 TestInitialize 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.

like image 517
BigBrother Avatar asked Nov 21 '09 18:11

BigBrother


People also ask

Where should test files go?

By convention, Go testing files are always located in the same folder, or package, where the code they are testing resides.

How do I run a unit test file?

To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).

How do I create a unit test file?

To get started, select a method, a type, or a namespace in the code editor in the project you want to test, right-click, and then choose Create Unit Tests. The Create Unit Tests dialog opens where you can configure how you want the tests to be created.

Should unit tests read files?

Strictly speaking, unit tests should not use the file system because it is slow. However, readability is more important. XML in a file is easier to read, and can be loaded in an XML friendly editor.


2 Answers

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.

like image 146
Mikael Svenson Avatar answered Oct 18 '22 21:10

Mikael Svenson


(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); } 
like image 45
DaniCE Avatar answered Oct 18 '22 22:10

DaniCE