How to get MSTest to Deploy a Subdirectory for Testing (Revised)

Published 15 July 09 01:26 PM | jons 

I am working on a project that uses MSTest for unit (and integration) testing.  In the main project (Win Forms) there is a subdirectory that holds some data files that are needed by the program and several of the unit tests.  Remember that MsTest creates a “shadow subdirectory” for each test run that contains everything that the test needs to run and reports that path as the base application path.  For our tests to run, we need to create a Data subdirectory that holds copies of these data files.  To make these data files available to the unit tests, we added some lines to the solution testrunconfig file:

   1: <Deployment>
   2:     <DeploymentItem filename="DeploymentMain\Data\File1.txt" outputDirectory="Data" />
   3: </Deployment>

The intent here was that File1.txt (plus several other similar files) would be copied to the Data subdirectory under the out directory for the test run.  It did not happen.  MsTest copied the file directly to the Out directory and did not create a Data subdirectory.  All of the unit tests that depended upon the existence of a fully stocked Data subdirectory failed. 

I looked for answers on the web with no luck.  To make a long morning’s work short, I created a small test solution and tried all of the variations that I would think of.  I finally stumbled on to the following "trick”: you cannot copy a file to a subdirectory but you can copy a subdirectory to a subdirectory.  The following deployment lines accomplish just that:

   1: <Deployment>
   2:   <DeploymentItem filename="DeploymentMain\Data\" outputDirectory="Data" />
   3: </Deployment>

Note that the back slash at the end of the file name attribute value is essential as is the “outputDirectory” attribute for this to work.

UPDATE: An additional point: it seems that the directory lines need to be first in the <Deployment> section.  I had a situation where the line was not first and it was as if the “outputDirectory” attribute was not there. 

And now you know.

New Comments to this post are disabled

About jons

Jon Stonecash is a technology consultant and has been designing, developing, and testing various kinds of software for such a long time that he has had the opportunity to make most of the serious software development mistakes at least once. His long term interests center about databases and the aspects of the application that handle data access and business logic. He is also interested in the tools that assist the development process, particularly code generation.