DeploymentItem Attribute

I was writing some code today that uses a xml file in my project, I setup a test project and tried to run it and of course I was getting file not found errors. I spent sometime fooling around with Visual Studio and the build actions, copying the file to the test project and changing build actions, spent alot of time on Google trying to figure this out.

Maybe it’s just been too long since I worked with files but eventually I came across the DeploymentItem Attribute.

[TestClass]
[DeploymentItem(@"C:\some\folders\project\Needed\my.xml", "Needed")]
public class ReportRepositoryTest
{
    ...
}

The first Parameter is the location of the file, the second and optional parameter is a folder to place the file in so you make the test project match up to the same relative locations as the actual project. When you execute your test if you view the Out\ folder you will see Out\Needed\my.xml

This is by far the simplest solution for your test project to access files needed by your application, especially since you won’t need to copy the files any where or deal with sharing files.

BloggingContext.ApplicationInstance.CompleteRequest();