Share via


DeploymentItemAttribute 类

用于指定部署项,如每个测试部署的文件或目录。 此类不能被继承。

继承层次结构

System.Object
  System.Attribute
    Microsoft.VisualStudio.TestTools.UnitTesting.DeploymentItemAttribute

命名空间:  Microsoft.VisualStudio.TestTools.UnitTesting
程序集:  Microsoft.VisualStudio.QualityTools.UnitTestFramework(在 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 中)

语法

声明
<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

DeploymentItemAttribute 类型公开以下成员。

构造函数

  名称 说明
公共方法 DeploymentItemAttribute(String) 初始化 DeploymentItemAttribute 类的新实例。将使用部署项或目录的路径来初始化使用此构造函数创建的对象。
公共方法 DeploymentItemAttribute(String, String) 初始化 DeploymentItemAttribute 类的新实例。将使用部署项和输出目录的路径来初始化使用此构造函数创建的对象。

页首

属性

  名称 说明
公共属性 OutputDirectory 获取表示输出目录的路径的值。
公共属性 Path 获取表示部署项的路径的值。
公共属性 TypeId 当在派生类中实现时,获取该 Attribute 的唯一标识符。 (继承自 Attribute。)

页首

方法

  名称 说明
公共方法 Equals 基础结构。返回一个值,该值指示此实例是否与指定的对象相等。 (继承自 Attribute。)
受保护的方法 Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
公共方法 GetHashCode 返回此实例的哈希代码。 (继承自 Attribute。)
公共方法 GetType 获取当前实例的 Type。 (继承自 Object。)
公共方法 IsDefaultAttribute 当在派生类中重写时,指示此实例的值是否是派生类的默认值。 (继承自 Attribute。)
公共方法 Match 当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。 (继承自 Attribute。)
受保护的方法 MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
公共方法 ToString 返回表示当前对象的字符串。 (继承自 Object。)

页首

显式接口实现

  名称 说明
显式接口实现私有方法 _Attribute.GetIDsOfNames 将一组名称映射为对应的一组调度标识符。 (继承自 Attribute。)
显式接口实现私有方法 _Attribute.GetTypeInfo 检索对象的类型信息,然后可以使用该信息获取接口的类型信息。 (继承自 Attribute。)
显式接口实现私有方法 _Attribute.GetTypeInfoCount 检索对象提供的类型信息接口的数量(0 或 1)。 (继承自 Attribute。)
显式接口实现私有方法 _Attribute.Invoke 提供对某一对象公开的属性和方法的访问。 (继承自 Attribute。)

页首

备注

根据您的测试设置,Microsoft Visual Studio 2010 将在您创建了这些设置的文件夹或在单独的部署文件夹中运行测试。 有关测试设置的更多信息,请参见创建测试设置以从 Visual Studio 中运行自动测试

本节介绍 Microsoft Visual Studio 2010 在与保存所生成程序集的文件夹不同的文件夹中运行测试的情况。

以这种方式运行测试时,测试和测试代码程序集以及部署项将放在该测试运行独有的测试部署文件夹中。 此特性标识文件和目录,这些目录包含要运行的部署测试所使用的文件。 测试引擎生成部署项的副本,并根据指定的 OutputDirectory 将这些副本放在测试部署目录或默认目录中。

可以在测试方法或测试类中指定此特性。 但是,派生类不继承此特性。

可以使用此特性的多个实例来指定多个项。 项路径可以是绝对路径或相对路径。 与项目路径相对的相对路径。

下面的示例演示 DeploymentItemAttribute 的不同用法:

  • [DeploymentItem("file1.xml")]   部署位于项目路径的名为 file1.xml 的项。 此文件部署到部署根目录。

  • [DeploymentItem("file2.xml", "DataFiles")]   部署位于项目路径的名为 file2.xml 的项。 此文件部署到部署根目录的 DataFiles 子目录。

  • [DeploymentItem("C:\\MyDataFiles\\")]    部署 MyDataFiles 目录中的所有项和目录。 这不会在部署目录下创建 MyDataFiles 目录。 MyDataFiles 中的所有文件和目录将部署到部署根目录。 若要复制整个 MyDataFiles 目录结构,必须将 MyDataFiles 指定为输出目录。

  • [DeploymentItem("%myDir%\myFile.txt")]   如果 myFile.txt 文件存在于 %myDir% 所解析成的目录中,则部署该文件。

有关如何使用特性的更多信息,请参见利用特性扩展元数据

示例

下面的类创建一个文件,此文件将由测试方法使用。

using System;
using System.IO;

namespace CarMaker
{
    public class Car
    {
        private static string make = "myMake";
        private static string model = "myModel";

        public static void CarInfo()
        {
            using (StreamWriter sw = new StreamWriter("testFile1.txt"))
            {
                sw.WriteLine(make);
                sw.WriteLine(model);
            }
        }
    }
}
Imports System
Imports System.IO

Namespace CarMaker
    Public Class Car

        Private Shared make As String = "myMake"
        Private Shared model As String = "myModel"

        Public Shared Sub CarInfo()
            Dim sw As New StreamWriter("testFile1.txt")
            Try
                sw.WriteLine(make)
                sw.WriteLine(model)
            Finally
                sw.Close()
            End Try
        End Sub
    End Class
End Namespace

下面的测试类包含一个测试方法,此方法将实例化 Car 类,Car 类将创建一个名为“testFile1.txt”的文件。 此文件将按 DeploymentItemAttribute 中所标识的方法进行部署。 然后,测试方法将测试此文件是否与测试程序集存在于同一目录中。

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

namespace DeploymentTest
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod()]
        [DeploymentItem("testFile1.txt")]
        public void ConstructorTest()
        {
            // Create the file to deploy
            Car.CarInfo();
            string file = "testFile1.txt";
            // Check if the created file exists in the deployment directory
            Assert.IsTrue(File.Exists(file), "deployment failed: " + file +
                " did not get deployed");
        }
    }
}
Imports System
Imports System.IO
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports MyVBProject.CarMaker

Namespace DeploymentTest
    <TestClass()> _
    Public Class UnitTest1
        <TestMethod()> _
        <DeploymentItem("testFile1.txt")> _
        Sub ConstructorTest()
            Car.CarInfo()
            Dim file As String = "testFile1.txt"
            Assert.IsTrue(IO.File.Exists(file), "deployment failed: " + file + _
                " did not get deployed")
        End Sub
    End Class
End Namespace

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

请参见

参考

Microsoft.VisualStudio.TestTools.UnitTesting 命名空间