Sep 23, 2009

Unit Testing: Deploying Additional Files

Visual Studio 2008 makes a lot of aspects of unit testing a breeze. Deploying additional files and folders is reasonably intuitive as long as you can use them at the root folder of your application. Unfortunately, when I deploy .xml data files with my application I usually group them under an XmlData directory. The normal deployment options cause my tests to fail because the files can't be found.

First, lets look at the easiest way to deploy additional files. If you did just want everything at the root folder you can use the Test menu and select option for editing the configuration as shown in the image. This will bring up the test configuration dialog where you can use the Add File button and navigate to the file you want to add. Don't forget to show all files as demonstrated in the image. 

When you want to deploy to a different folder you have to use the DeploymentItem  attribute. It takes a second parameter that allows you to specify a subfolder where you want the item deployed. In my case I just want to maintain the same relative folder path so that I don't need to change code just to make my unit tests work.
Here is the same example file being added to an XmlData subfolder using the DeploymentItem attribute method.


About Me

My photo
Tod Gentille (@todgentille) is now a Curriculum Director for Pluralsight. He's been programming professionally since well before you were born and was a software consultant for most of his career. He's also a father, husband, drummer, and windsurfer. He wants to be a guitar player but he just hasn't got the chops for it.