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.
Assembly: Microsoft.VisualStudio.QualityTools.UnitTestFramework (in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)
The AssemblyCleanupAttribute type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | AssemblyCleanupAttribute | Initializes a new instance of the AssemblyCleanupAttribute class. |
| Name | Description | |
|---|---|---|
![]() | Equals | Infrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.) |
![]() | GetHashCode | Returns the hash code for this instance. (Inherited from Attribute.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | 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.) |
![]() | Match | When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
| Name | Description | |
|---|---|---|
![]() ![]() | System#Runtime#InteropServices#_Attribute#GetIDsOfNames | Maps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.) |
![]() ![]() | 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.) |
![]() ![]() | System#Runtime#InteropServices#_Attribute#GetTypeInfoCount | Retrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.) |
![]() ![]() | System#Runtime#InteropServices#_Attribute#Invoke | Provides access to properties and methods exposed by an object. (Inherited from 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.
Important |
|---|
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.
using System; namespace SampleClassLib { public class DivideClass { public static int DivideMethod(int denominator) { return (2 / denominator); } } }
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.
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); } } }




Important