Identifies a method that contains code that must be used before any of the tests in the test class have run and to allocate resources to be used by the test class. This class cannot be inherited.
Assembly: Microsoft.VisualStudio.QualityTools.UnitTestFramework (in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)
|_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)|
|_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)|
|_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)|
When run in a load test, the method marked with this attribute will run once, and any initialization operations it performs will apply to the entire test. If you need to do initialization operations once for every virtual user iteration in the test, use the TestInitializeAttribute.
The order that methods will be run is:
Methods marked with the AssemblyInitializeAttribute.
Methods marked with the.
Methods marked with the TestInitializeAttribute.
Methods marked with the TestMethodAttribute.
Only one method in a class may be decorated with this attribute.
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 may 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.
Legacy Code Example
The following examples demonstrate the initialization and clean-up attributes 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.
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 ClassInitialize attribute on the ClassInit()method.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.