p.campbell p.campbell - 1 month ago 5x
C# Question

Visual Studio - Unit tests loading resources in the project

The goal is to run some tests given some data in those Xml files.

How would you easily load a given Xml file into an XmlDoc within the unit test methods?

Current state is:

XmlDocument doc = new XmlDocument();
string xmlFile = "4.xml";
string dir = System.IO.Directory.GetCurrentDirectory() + @"\Msgs\"

//dir is then the value of the current exe's path, which is
//d:\sourcecode\myproject\TestResults\myComputer 2009-10-08 16_07_45\Out

//we actually need:
doc.Load( dir + fileName); //should really use System.IO.Path.Combine()!

Is it just a simple matter of putting that path in an
? I was hoping to avoid that, given the possibility of different paths on developer machines.

Question: How would you write the algorithm to load a given Xml file into an XmlDocument in the unit test method?


In the unit test project add a post-build event that copies the XML file to the output directory. Then, you can use your original code to get the XML file.

The post build event will look like something like this:

copy $(SolutionDir)file.xml $(ProjectDir)$(OutDir)file.xml

You may also need this to add to your path: