Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get path of project from test project?

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:

  • Build Action: Content
  • Copy to Output Directory: Always

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
like image 379
ford Avatar asked Jan 05 '12 18:01

ford


1 Answers

you could set a variable to get the path of where the application is being launched from

var execPath = AppDomain.CurrentDomain.BaseDirectory;
like image 189
MethodMan Avatar answered Oct 06 '22 22:10

MethodMan