Visual Studio - Unit tests loading resources in th

2020-02-26 03:47发布

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:
  //d:\sourcecode\myproject\Msgs\ 
  doc.Load( dir + fileName); //should really use System.IO.Path.Combine()!

Is it just a simple matter of putting that path in an app.config? 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?

7条回答
孤傲高冷的网名
2楼-- · 2020-02-26 04:32

You can build those files into your executable (set their "Build Action" property to "Embedded Resource") and then get them using the Assembly.GetManifestResourceStream method.

查看更多
登录 后发表回答