Using Embedded Resources for Unit Testing…

I’ve often had to use XML documents in my unit testing. In the past, I’ve always done a concatenation to string together a document.

Xml document concat

However, I came across a post on Haacked that lead me to a better solution, embedded resources. Basically, you add a document to your project and mark it as an “Embedded Resource” rather than “Content” in the “Build Action” attribute.

Mark resource as embedded instead of content

This will pack it into the unit testing dll and allow you to extract it from the assembly. No more missing documents causing failing tests! In my custom class, I’ve defined a method for unwrapping the resource that allows you to pass in arguments that it will use for customizing the xml nodes. I found this to be simpler than attempting to navigate the document and update the nodes although I’m guessing it may be less performant.

Unwrap Method

One thing to be sure of with this, is that you use a centralized location for the resources so that your lookup doesn’t fail. So now I’m able to write my tests such that their intent is clear and the code is clean.

Resourced Test

Although I haven’t had the need yet, I could see where this could easily grow into handling multiple types of documents but I’ll hold off on that code until I need it. 🙂

Advertisements

5 Responses to Using Embedded Resources for Unit Testing…

  1. Tim Rayburn says:

    Dave,
    I write about a very similar problem, solved in a similar way, back in Feb 2006. I often have the situation that I need to access a file on disk for my code. So I wrote an IDisposable wrapper that uses Embedded Resources just as you describe. Details can be found here:
    http://www.timrayburn.net/2006/02/19/ExternalFileDependencyInNUnit.aspx

    Good content! Keep it coming!
    Tim

  2. Ghosty says:

    Interesting that you posted this, as I have just started teaching myself XML this past week … I am surprised at the flexibility and versatility of the language.

  3. Chris Morris says:

    We do embedded resources for our unit tests at eInstruction. Some of them are fat, and we started having problems with memory usage inside Visual Studio with R#, so we now have a separate statically referenced assembly with all our test resources in it.

  4. finalkut says:

    Here’s a question: how would you unit test your UnwrapXmlResource method?

    I have a very similar method – though it is static – in a standard library – and I have pretty good test coverage on everything except for the extraction of an embedded resource.

    Because the method I’m testing is more utilitarian than just for unit testing it expects the embedded resource to be in the “entry assembly” so I use Assembly.GetEntryAssembly().GetManifestResourceStream(resource);

    However, with a unit test there is no “EntryAssembly”

    Any thoughts?

  5. David O'Hara says:

    finalkut: I suppose you could continue to pull pieces out (Assembly, Stream) until you get the thinnest layer possible but at some point you’d simply have to have an integration test that gets you that “last mile”. For me, I’m good with leaving it untested since it’s really only used for testing itself and I’m not interested in falling into “testing recursion”.

%d bloggers like this: