Export (0) Print
Expand All

AssemblyCleanupAttribute Class

Identifies a method that contains code to be used after all tests in the assembly have run and to free resources obtained by the assembly. This class cannot be inherited.

Namespace: Microsoft.VisualStudio.TestTools.UnitTesting
Assembly: Microsoft.VisualStudio.QualityTools.UnitTestFramework (in microsoft.visualstudio.qualitytools.unittestframework.dll)

'Declaration
<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple:=False)> _
Public NotInheritable Class AssemblyCleanupAttribute
	Inherits Attribute
'Usage
Dim instance As AssemblyCleanupAttribute

/** @attribute AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple=false) */ 
public final class AssemblyCleanupAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple=false) 
public final class AssemblyCleanupAttribute extends Attribute

The method marked with this attribute will be run after methods marked with the TestCleanupAttribute and the ClassCleanupAttribute attributes. This will not execute if an unhandled exception is thrown. A method that has an AssemblyCleanupAttribute attribute will execute if it is not in the same class as the test method. Only one method in an assembly may be decorated with this attribute.

NoteImportant

This attribute should not be used on ASP.NET unit tests, that is, any test with [HostType("ASP.NET")] attribute. Because of the stateless nature of IIS and ASP.NET, a method decorated with this attribute might be called more than once per test run.

This attribute can be specified on a method. Only one instance of this attribute may be applied to a method.

For more information about how to use attributes, see Extending Metadata Using Attributes.

The following examples demonstrate the initialization and clean-up attributes that are used to indicate which methods should be run by the test engine at different periods of the test.

The first code samples contain a class and method to test. To run this example, create a class library project and replace the code with the following example.

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

The following example contains code to test DivideMethod() found in the previous code examples. Create a test project and put the following code in a test class document. Add the appropriate references to the project. This code contains attributes that control the initialization and clean-up execution order for the method, class, and assembly.

In particular, note the AssemblyCleanup attribute on the AssemblyCleanup()method.

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

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

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

Community Additions

ADD
Show:
© 2014 Microsoft