共用方式為


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 的淺層複本 (Shallow Copy)。 (繼承自 Object)。
公用方法 ToString 傳回表示目前物件的字串。 (繼承自 Object)。

回頁首

明確介面實作

  名稱 說明
明確介面實作私用方法 _Attribute.GetIDsOfNames 將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。 (繼承自 Attribute)。
明確介面實作私用方法 _Attribute.GetTypeInfo 擷取物件的型別資訊,可以用來取得介面的型別資訊。 (繼承自 Attribute)。
明確介面實作私用方法 _Attribute.GetTypeInfoCount 擷取物件提供的型別資訊介面數目 (0 或 1)。 (繼承自 Attribute)。
明確介面實作私用方法 _Attribute.Invoke 提供物件所公開的屬性和方法的存取權。 (繼承自 Attribute)。

回頁首

備註

根據您的測試設定,Microsoft Visual Studio 2010 會在建立測試的資料夾中,或是在不同的 "deployment" 資料夾中執行測試。 如需測試設定的詳細資訊,請參閱建立測試設定以便從 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 類別,此類別將建立一個名為 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

執行緒安全

這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。

請參閱

參考

Microsoft.VisualStudio.TestTools.UnitTesting 命名空間