Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

TestInitializeAttribute Class

 

Identifies the method to run before the test to allocate and configure resources needed by all tests in the test class. This class cannot be inherited.

Namespace:   Microsoft.VisualStudio.TestTools.UnitTesting
Assembly:  Microsoft.VisualStudio.QualityTools.UnitTestFramework (in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)

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

No code example is currently available or this language may not be supported.

NameDescription
System_CAPS_pubmethodTestInitializeAttribute()

Initializes a new instance of the TestInitializeAttribute class.

NameDescription
System_CAPS_pubpropertyTypeId

(Inherited from Attribute.)

NameDescription
System_CAPS_pubmethodEquals(Object)

(Inherited from Attribute.)

System_CAPS_pubmethodGetHashCode()

(Inherited from Attribute.)

System_CAPS_pubmethodGetType()

(Inherited from Object.)

System_CAPS_pubmethodIsDefaultAttribute()

(Inherited from Attribute.)

System_CAPS_pubmethodMatch(Object)

(Inherited from Attribute.)

System_CAPS_pubmethodToString()

(Inherited from Object.)

When run in a load test, the method marked with this attribute will run once for every virtual user iteration in the test. If you need to do initialization operations once, that apply to the entire test, use the ClassInitializeAttribute.

The order that methods will be run is:

  1. Methods marked with the AssemblyInitializeAttribute.

  2. Methods marked with the ClassInitializeAttribute.

  3. Methods marked with the TestInitializeAttribute.

  4. Methods marked with the TestMethodAttribute.

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

This attribute is used by default in generated code.

For more information about using attributes, see Extending Metadata Using Attributes.

Legacy Code Example

The SampleClassLib namespace contains the DivideClass class, which in turn contains the method we want to test. This method is called DivideMethod().

No code example is currently available or this language may not be supported.

In the following code, the DivideClassTest test class contains a test method called DivideMethodTest. This code also contains attributes that control the initialization and clean-up execution order for the method, class, and assembly.

In particular, note the TestInitialize attribute on the Initialize() method.

No code example is currently available or this language may not be supported.

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

Return to top
Show: