Table of contents for Visual Studio and MSTest

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

Microsoft’s unit testing infrastructure provides a number of options for deploying files to the test directory when running tests.

  • Deploy files/directories for all tests in all test projects.
  • Deploy files/directories for a specific test, specified in the UI.
  • Deploy files/directories for all tests in a test class, specified in code.
  • Deploy files/directories for a specific test, specified in code.

All tests in all test projects

The Deployment page of the test run configuration dialog not only allows you to specify whether you want to deploy to a separate test directory for each test run. It also allows you to specify files and directories to deploy when a test is run. The configuration page allows you to specify individual files as well as directories. This configuration is stored in the .testrunconfig file.

Specific test in UI

You can specify files and directories to deploy on a test-by-test basis as well, stored in the test run configuration file.

  1. Select a test in the Test View window or in the Test List Editor.
  2. Press F4 to view the properties of the test.
  3. Click the Deployment Items property. An ellipses (…) appears in the value column.
  4. Click the ellipses. The String Collection Editor dialog box is displayed.
  5. Specify files and folders to deploy, one per line.

You’ll note that by adding deployment items in this way, Visual Studio will actually add DeploymentItem attributes to your code for the test.

Test class or specific test in code

Microsoft provides the DeploymentItem attribute for specifying items to deploy in code. This is a good solution if you have tests that need different sets of files to be deployed. The documentation and quite a few blog posts say that this attribute only works on methods marked with the TestMethod attribute, but I found that it also works on classes marked with the TestClass attribute.

There are two flavors of this attribute. The first allows you to specify a source path for a file or directory. As specified above, you can specify an absolute or a relative path to the file or directory. Also as above, file paths are relative to the test project while directory paths are relative to the solution.

The second flavor also allows you to specify a destination directory. The destination directory is relative to the test directory.


// Deploy all files in the OtherProject\dll directory to the test directory.
public class MyProjectTest
    // Deploy test.xsl to the xsl subdirectory below the test directory.
    [DeploymentItem(@"..\OtherProject\test.xsl", "xsl")]
    public void Test1()


You can specify absolute or relative paths. Microsoft’s MSDN article on this subject says that relative paths are relative to the RelativePathRoot setting found in the .testrunconfig file. However, what if that setting isn’t set? It turns out to be different for files and for directories. For files it is relative to the test project. For directories it is relative to the solution. Sure would be nice if they would be consistent.

To specify a directory, end the line with a backslash.

When deploying a directory, it doesn’t preserve the source directory structure. It simply copies all files from all directories and subdirectories into the test directory.

You can use environment variables in paths. However, you can’t use Visual Studio macros such as $(ConfigurationName) in paths. This is unfortunate as that leaves no way to deploy configuration-specific files to the test directory.

Ade Miller wrote a good article on some of the gotchas of using the DeploymentItem attribute. The article is a good read and addresses handling deployment failures, duplicate file names, and how to handle tests that change deployed files.