共用方式為


AssemblyCleanupAttribute 建構函式

初始化 AssemblyCleanupAttribute 類別的新執行個體。

命名空間:  Microsoft.VisualStudio.TestTools.UnitTesting
組件:  Microsoft.VisualStudio.QualityTools.UnitTestFramework (在 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 中)

語法

'宣告
Public Sub New
public AssemblyCleanupAttribute()
public:
AssemblyCleanupAttribute()
new : unit -> AssemblyCleanupAttribute
public function AssemblyCleanupAttribute()

範例

下列範例將示範初始化和清除屬性,這些屬性是用來表示在測試的不同時期,測試引擎應該執行哪些方法。

第一組程式碼範例包含要測試的類別和方法。若要執行這個範例,請建立類別庫專案,並以下列範例取代其程式碼。

using System;

namespace SampleClassLib
{
    public class DivideClass
    {
        public static int DivideMethod(int denominator)
        {
            return (2 / denominator);
        }
    }
}
Imports System

Namespace SampleClassLib
    Public Class DivideClass
        Shared Function DivideMethod(ByVal denominator As Integer) As Integer
            Return 2 \ denominator
        End Function
    End Class
End Namespace

下列範例所包含的程式碼可測試在先前程式碼範例中找到的 DivideMethod()。建立測試專案,並將下列程式碼放到測試類別文件中。將適當的參考加入至專案中。這個程式碼包含控制方法、類別和組件之初始化及清除執行順序的屬性。

請特別注意 AssemblyCleanup() 方法上的 AssemblyCleanup 屬性。

using Microsoft.VisualStudio.TestTools.UnitTesting;
using SampleClassLib;
using System;
using System.Windows.Forms;

namespace TestNamespace
{
    [TestClass()]
    public sealed class DivideClassTest
    {
        [AssemblyInitialize()]
        public static void AssemblyInit(TestContext context)
        {
            MessageBox.Show("AssemblyInit " + context.TestName);
        }

        [ClassInitialize()]
        public static void ClassInit(TestContext context)
        {
            MessageBox.Show("ClassInit " + context.TestName);
        }

        [TestInitialize()]
        public void Initialize()
        {
            MessageBox.Show("TestMethodInit");
        }

        [TestCleanup()]
        public void Cleanup()
        {
            MessageBox.Show("TestMethodCleanup");
        }

        [ClassCleanup()]
        public static void ClassCleanup()
        {
            MessageBox.Show("ClassCleanup");
        }

        [AssemblyCleanup()]
        public static void AssemblyCleanup()
        {
            MessageBox.Show("AssemblyCleanup");
        }

        [TestMethod()]
        [ExpectedException(typeof(System.DivideByZeroException))]
        public void DivideMethodTest()
        {
            DivideClass.DivideMethod(0);
        }
    }
}
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports SampleClassLib2.SampleClassLib
Imports System
Imports System.IO
Imports System.Windows.Forms

Namespace TestNamespace
    <TestClass()> _
    Public NotInheritable Class DivideClassTest
        <AssemblyInitialize()> _
        Public Shared Sub AssemblyInit(ByVal context As TestContext)
            MsgBox("AssemblyInit " + context.TestName)
        End Sub 'AssemblyInit

        <ClassInitialize()> _
        Public Shared Sub ClassInit(ByVal context As TestContext)
            MsgBox("ClassInit " + context.TestName)
        End Sub 'ClassInit

        <TestInitialize()> _
        Public Sub Initialize()
            MsgBox("TestMethodInit")
        End Sub 

        <TestCleanup()> _
        Public Sub Cleanup()
            MsgBox("TestMethodCleanup")
        End Sub 

        <ClassCleanup()> _
        Public Shared Sub ClassCleanup()
            MsgBox("ClassCleanup")
        End Sub 

        <AssemblyCleanup()> _
        Public Shared Sub AssemblyCleanup()
            MsgBox("AssemblyCleanup")
        End Sub

        <TestMethod()> _
        <ExpectedException(GetType(System.DivideByZeroException))> _
        Public Sub DivideMethodTest()
            DivideClass.DivideMethod(0)
        End Sub
    End Class
End Namespace

.NET Framework 安全性

請參閱

參考

AssemblyCleanupAttribute 類別

Microsoft.VisualStudio.TestTools.UnitTesting 命名空間