views:

56

answers:

2

I have an XML file that I want to base some unit tests off of. Currently I load the XML file from disk in the class initialize method. I would rather have this xml generated in the test instead of reading the file from disk. Are there any utilities that will automatically generate the linqtoxml code to generate a given XML file? Or are there better ways to do this? Is loading from disk ok for unit tests?

A: 

Probably it's better to use some resource file, e.g. resx where you put the xml as a string resource. That's fast enough for a unit test and you don't have to do any magic. Reading from disk is not ok for various reasons (speed, need for configuration etc.)

Thomas Wanner
+1  A: 

I would embed the XML file directly into the assembly - no need for a string resource or anything like that, just include it as an embedded resource (create a file, go to the properties in Visual Studio, and select "Embedded Resource").

Then you can read it using Assembly.GetManifestResourceStream, load the XML from that as you would any other stream, and you're away.

I've used this technique several times - it makes it a lot easier to see the data you're interested in.

Jon Skeet