Bagikan melalui


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.

Inheritance Hierarchy

Object
  Attribute
    Microsoft.VisualStudio.TestTools.UnitTesting.DeploymentItemAttribute

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

Syntax

'Declaration
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Method, AllowMultiple := True)> _
Public NotInheritable Class DeploymentItemAttribute _
    Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, AllowMultiple = true)]
public sealed class DeploymentItemAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Class|AttributeTargets::Method, AllowMultiple = true)]
public ref class DeploymentItemAttribute sealed : public Attribute
[<Sealed>]
[<AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, AllowMultiple = true)>]
type DeploymentItemAttribute =  
    class 
        inherit Attribute 
    end
public final class DeploymentItemAttribute extends Attribute

The DeploymentItemAttribute type exposes the following members.

Constructors

  Name Description
Public method DeploymentItemAttribute(String) Specifies an item to be deployed before a test run starts.
Public method DeploymentItemAttribute(String, String) Specifies an item to be deployed before a test run starts.

Top

Properties

  Name Description
Public property OutputDirectory Gets the path of the directory to which the item is copied.
Public property Path Gets the path of the source file or folder to be copied.
Public property TypeId When implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)

Top

Methods

  Name Description
Public method Equals Infrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Public method GetHashCode Returns the hash code for this instance. (Inherited from Attribute.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method 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.)
Public method Match When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate method System#Runtime#InteropServices#_Attribute#GetIDsOfNames Maps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetationPrivate method 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.)
Explicit interface implemetationPrivate method System#Runtime#InteropServices#_Attribute#GetTypeInfoCount Retrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetationPrivate method System#Runtime#InteropServices#_Attribute#Invoke Provides access to properties and methods exposed by an object. (Inherited from Attribute.)

Top

Remarks

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 .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:

  • [DeploymentItem("file1.xml")]
    Copies file1.xml from the build output directory to the deployment directory.

  • [DeploymentItem(@"Testfiles\")]
    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.

    Note

    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.

  • [DeploymentItem(@"C:\MyDataFiles\")]
    Copies the contents of the MyDataFiles folder into the deployment folder.

  • (If you use a .testsettings 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.

Examples

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 ...
        }
    }
}
Imports System
Imports System.IO
Imports Microsoft.VisualStudio.TestTools.UnitTesting

Namespace DeploymentTest
    <TestClass()> _
    Public Class UnitTest1
        <TestMethod()> _
        <DeploymentItem("test1.xml")> _
        <DeploymentItem("test2.xml", "Data")> _
        <DeploymentItem("Resources\test3.xml")> _
        <DeploymentItem("Resources\test4.xml", "Data")> _
        Sub ConstructorTest()
            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 ...
        End Sub
    End Class
End Namespace

Thread Safety

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

See Also

Reference

Microsoft.VisualStudio.TestTools.UnitTesting Namespace