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 LINQ to XML code to generate a given XML file?
Or are there better ways to do this? Is loading from disk OK for unit tests?
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.