DeploymentItemAttribute Class
Collapse the table of content
Expand the table of content

DeploymentItemAttribute Class


Specify a file or directory that should be deployed along with the assemblies before running a test. Attach this attribute to a test class or test method. You can use multiple instances. This attribute is not inherited.

Namespace:   Microsoft.VisualStudio.TestTools.UnitTesting
Assembly:  Microsoft.VisualStudio.QualityTools.UnitTestFramework (in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)


[AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Method, 
	AllowMultiple = true)]
public sealed class DeploymentItemAttribute : Attribute


Specifies an item to be deployed before a test run starts.

System_CAPS_pubmethodDeploymentItemAttribute(String, String)

Specifies an item to be deployed before a test run starts.


Gets the path of the directory to which the item is copied.


Gets the path of the source file or folder to be copied.


(Inherited from Attribute.)


(Inherited from Attribute.)


(Inherited from Attribute.)


(Inherited from Object.)


(Inherited from Attribute.)


(Inherited from Attribute.)


(Inherited from Object.)

Visual Studio 2012 runs tests either in the folder in which you have built them or in a separate deployment folder that is unique to the test run. If a deployment folder is used, the test engine creates a deployment folder and copies into it the assemblies containing the test code, the application, and any assemblies they reference.

But some tests require additional files, such as test data, configuration files, databases, or explicitly loaded assemblies. To make these files available during the test, you have to specify that they should be copied along with the test assemblies. Here’s the best way to do this:

  1. Copy the files to the build target directory as part of the build process.

    • If they are specific to one test project, include them as content files in the Visual Studio test project. Select them in Solution Explorer and set the Copy to Output property to Copy if Newer.

    • Otherwise, define a post-build task to copy the files into the build output directory. For example:

      xcopy /Y /S "$(SolutionDir)SharedFiles\*" "$(TargetDir)"

      Open the project properties of your test project. In a C# project, open the Build Events page. In a Visual Basic project, open the Compile page and choose Build Events. Add the copy command to the Post-build event field.

  2. Use DeploymentItemAttribute on test methods or test classes to specify the files and folders that should be copied from the build output directory to the deployment directory.

  3. Consider running your unit tests directly in the build output directory, so that testing runs more rapidly. This is especially useful on the build server after you have checked in your tests. To do this, add a file to your solution, include <DeploymentEnabled>False</DeploymentEnabled>, and select the file in the Test, Test Settings menu. The same effect occurs in any test run in which DeploymentItemAttribute is not used at all.

    However, you might prefer not to do this if you want to be able to inspect the data files after a failed run.

    You cannot avoid using a deployment folder if you are using a file, which is required for web and load tests, coded UI tests, and any test in which you deploy an application to remote machines.

In a test run, all the items in the tests that are to be run are deployed before any test is started.

For more information, see How to: Deploy Files for Tests.

DeploymentItemAttribute has two parameters:

  • Source item path is relative to the build output folder. It can be a file or folder. To avoid dependency on your project structure, move the item into your build output directory as part of the build process. Use the deployment item attribute to deploy it from there.

  • (Optional) Target directory path must be a folder, and it is relative to the deployment directory. If the folder does not exist, it will be created. The default value is the deployment directory.

    You cannot change the file name by using DeploymentItem.

The following examples demonstrate usage of the DeploymentItemAttribute:


Copies file1.xml from the build output directory to the deployment directory.


Copies all the files and folders in the Testfiles folder from the build output folder to the deployment folder. Subfolders are replicated in the deployment folder.

[DeploymentItem("file2.xml", "DataFiles")]

Creates a folder named DataFiles in the deployment folder, and copies file2.xml from the build output folder to DataFiles.


If you use the second parameter, it must always be the path of a folder, never a file. If the folder does not exist, it will be created. You cannot change the name of the file by using DeploymentItem.

[DeploymentItem(@"Resources\file2.xml", "DataFiles")]

Creates a folder named DataFiles in the deployment folder if it does not exist. Copies file2.xml from the Resources folder under the build output folder to DataFiles. Notice that the Resources folder is not duplicated in the destination folder.

[DeploymentItem(@"TestFiles\", "TestFiles")]

Copies the contents of TestFiles into a subfolder of the deployment folder. Subfolders are replicated under the destination.

[DeploymentItem(@"..\..\file1.xml")] (Not recommended)

Copies an item from the project directory. This example assumes the typical project structure in which the output directory is, for example, in bin\Debug.

Instead of relying on the project structure in this way, set the file’s Copy to Output property. Deploy the file from the build output directory.


Copies the contents of the MyDataFiles folder into the deployment folder.

(If you use a file) [DeploymentItem("%myDir%\myFile.txt")]

Deploys the file myFile.txt if that file exists in the directory to which %myDir% resolves.

For more information about how to use attributes, see Extending Metadata Using Attributes.

Legacy Code Example

The following test reads files named "test*.xml". To make the files available to the test and to the application under test, they are identified by using the DeploymentItemAttribute. The test method will verify that the files exist under the deployment directory, before going on to test the application.

using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DeploymentTest
    public class UnitTest1
        // Copy files from build directory:
        [DeploymentItem("test2.xml", "Data")]
        // Copy files from Resources subdirectory:
        [DeploymentItem("Resources\\test4.xml", "Data")]

        public void ConstructorTest()
            // Verify that the files exist in the deployment directory

            // Now test the application ...

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
© 2015 Microsoft