1

I have 3 projects in a solution, one is a ConsoleApp, the other two are a MyLibrary class library with one class, and a xUnit test project called Tests

I have also created a Configuration file in the ConsoleApp

The single class in the class library MyLibrary gets a reference to the configuration file in ConsoleApp and if you run the ConsoleApp the code works, but if you run the Tests it fails because the path changes to the Tests.

Here is the ConsoleApp:

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var myTestLibraryClass = new TestLibraryClass();

            Console.WriteLine("Hello, World!");
        }
    }
} 

Here is the single class in the class library:

namespace MyLibrary
{
    public class TestLibraryClass
    {
        public TestLibraryClass()
        {
            // get the path of the Configuration.config from the ConsoleApp
            string configpPath = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName, "Configuration.config");

            XmlDocument MyConfig = new XmlDocument();
            MyConfig.Load(File.OpenRead(configpPath));
        }
    }
}

Here is the class from the Tests:

namespace Tests
{
    public class MyTest
    {
        [Fact]
        public void MyPathTest()
        {
            var myTestLibraryClass = new TestLibraryClass();
        }
    }
}

I am guessing I can hardcode an absolute path string to the Configuration.config in the ConsoleApp in File.OpenRead("c:\Users\......") but I do not feel this is the right thing to do.

One way I got around this problem for now is to copy the Configuration.config into the Tests project, but again this does not feel like the right thing to do.

Is there a cleaner better solution out for this?

The current exception for this is:

System.IO.FileNotFoundException : Could not find file '/Users/.../.../TestPaths/Tests/Configuration.config'.

Which does make sense with my current code.

4

0 回答 0