I have a unit test project set up in the same solution as my project in Visual Studio. Unit testing is being done via built in Unit Testing tools in Visual Studio (included in Premium and above versions). I need to load a file that is in the path of the project itself, not the test project, while running unit tests in the test project.
The file to include is part of the main project, and has the following properties:
I need to write a unit test that for a function that depends on this file, or I will hit an error state and will not be able to write tests for 100% coverage on that function.
How would I get the execution path of the actual project from the unit test project?
Edit: The specific function reads all lines in the config file and stores them one at a time in a list. Sample code follows:
public List<string> LoadConfigFile() {
List<string> models = new List<string>();
StreamReader sr = new StreamReader(Path.GetDirectoryName(Application.ExecutablePath) + @"\" + Properties.Resources.SupportedModelsConfigFile);
while ((line = sr.ReadLine()) != null)
{
models.Add(line);
}
sr.Close();
return models;
}
Primary Problem: Application.ExecutablePath works fine when running the program inside or outside of the IDE, but when running unit tests, it sends me to a directory within visual studio, specifically this directory:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\QTAgent32.exe
you could set a variable to get the path of where the application is being launched from
var execPath = AppDomain.CurrentDomain.BaseDirectory;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With