Table of contents for Visual Studio and MSTest

  1. Visual Studio and MSTest: Introduction
  2. Visual Studio and MSTest: Deploying Files


I’m coming up to speed on my new company’s development environment, and part of that is the usage of Microsoft’s unit testing infrastructure built into Visual Studio 2008. One of the things the tests need to be able to do is load DLLs provided by a third party. However, the unit testing environment is (or rather, can be) different that the manual debugging environment. To set up for manual debugging, typically you add a post-build command to copy the files to the target directory, which is identified as $(TargetDir). The unit testing infrastructure, however, copies everything it thinks is needed to a new folder below the TestResults directory for each test run. You can, of course, choose to disable this behavior and just run your tests from the target directory like you do when you manually debug the project. However, there are a number of benefits to deploying them to a separate folder:

  1. You can be sure that each test run is working with the latest files that have been built. There won’t be any extra cruft available from a previous test run.
  2. Code coverage data is collected only when tests are deployed in this manner.
  3. Some tests won’t be run unless they are deployed to a separate folder, specifically smart device tests.

Of course, there are some gotchas as well with deploying to a separate directory:

  1. You have to copy external files in multiple places (one for manually debugging, one for test deployment).
  2. The mechanisms for copying files to a test deployment aren’t completely analogous to those for copying them for manual debugging. For example, there doesn’t appear to be any way to specify that you want to copy files from a matching configuration (e.g. Debug or Release).

To change how tests are run:

  1. Bring up the test configuration properties by clicking on Test -> Edit Test Run Configurations -> Local Test Run (or whatever your test run configuration is called).
  2. Select the Deployment item in the list on the left.
  3. Check the Enable deployment checkbox to enable deploying tests to a separate directory for each test run. Uncheck the checkbox to run tests out of the build target directory.

The configuration dialog box also allows you to specify files to deploy to the test directory when a test is run. File deployment is a topic that deserves its own article, however.

Resources