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.
Assembly: Microsoft.VisualStudio.QualityTools.UnitTestFramework (in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)
The DeploymentItemAttribute type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | DeploymentItemAttribute(String) | Specifies an item to be deployed before a test run starts. |
![]() | DeploymentItemAttribute(String, String) | Specifies an item to be deployed before a test run starts. |
| Name | Description | |
|---|---|---|
![]() | OutputDirectory | Gets the path of the directory to which the item is copied. |
![]() | Path | Gets the path of the source file or folder to be copied. |
![]() | TypeId | When implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.) |
| Name | Description | |
|---|---|---|
![]() | Equals | Infrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.) |
![]() | GetHashCode | Returns the hash code for this instance. (Inherited from Attribute.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | IsDefaultAttribute | When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.) |
![]() | Match | When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
| Name | Description | |
|---|---|---|
![]() ![]() | System#Runtime#InteropServices#_Attribute#GetIDsOfNames | Maps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.) |
![]() ![]() | System#Runtime#InteropServices#_Attribute#GetTypeInfo | Retrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.) |
![]() ![]() | System#Runtime#InteropServices#_Attribute#GetTypeInfoCount | Retrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.) |
![]() ![]() | System#Runtime#InteropServices#_Attribute#Invoke | Provides access to properties and methods exposed by an object. (Inherited from Attribute.) |
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:
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.
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.
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 .runsettings 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 .testsettings 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:
For more information about how to use attributes, see Extending Metadata Using Attributes.
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 { [TestClass] public class UnitTest1 { [TestMethod] // Copy files from build directory: [DeploymentItem("test1.xml")] [DeploymentItem("test2.xml", "Data")] // Copy files from Resources subdirectory: [DeploymentItem("Resources\\test3.xml")] [DeploymentItem("Resources\\test4.xml", "Data")] public void ConstructorTest() { // Verify that the files exist in the deployment directory Assert.IsTrue(File.Exists("test1.xml")); Assert.IsTrue(File.Exists("Data\\test2.xml")); Assert.IsTrue(File.Exists("test3.xml")); Assert.IsTrue(File.Exists("Data\\test4.xml")); // Now test the application ... } } }
