Partager via


ClassInitializeAttribute Class

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.

Inheritance Hierarchy

Object
  Attribute
    Microsoft.VisualStudio.TestTools.UnitTesting.ClassInitializeAttribute

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

Syntax

'Declaration
<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple := False)> _
Public NotInheritable Class ClassInitializeAttribute _
    Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple = false)]
public sealed class ClassInitializeAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Method, AllowMultiple = false)]
public ref class ClassInitializeAttribute sealed : public Attribute
[<Sealed>]
[<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple = false)>]
type ClassInitializeAttribute =  
    class 
        inherit Attribute 
    end
public final class ClassInitializeAttribute extends Attribute

The ClassInitializeAttribute type exposes the following members.

Constructors

  Name Description
Public method ClassInitializeAttribute Initializes a new instance of the ClassInitializeAttribute class.

Top

Properties

  Name Description
Public property TypeId When implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)

Top

Methods

  Name Description
Public method Equals Infrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Public method GetHashCode Returns the hash code for this instance. (Inherited from Attribute.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method 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.)
Public method Match When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate method System#Runtime#InteropServices#_Attribute#GetIDsOfNames Maps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetationPrivate method 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.)
Explicit interface implemetationPrivate method System#Runtime#InteropServices#_Attribute#GetTypeInfoCount Retrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetationPrivate method System#Runtime#InteropServices#_Attribute#Invoke Provides access to properties and methods exposed by an object. (Inherited from Attribute.)

Top

Remarks

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:

  1. Methods marked with the AssemblyInitializeAttribute.

  2. Methods marked with the ClassInitializeAttribute.

  3. Methods marked with the TestInitializeAttribute.

  4. Methods marked with the TestMethodAttribute.

Only one method in a class 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 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.

Examples

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.

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

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.

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

Thread Safety

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

See Also

Reference

Microsoft.VisualStudio.TestTools.UnitTesting Namespace

Other Resources

Structure of Unit Tests

Unit Testing Framework