Share via


AssemblyInitializeAttribute Constructor

Initializes a new instance of the AssemblyInitializeAttribute class.

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

Syntax

'Declaration
Public Sub New
public AssemblyInitializeAttribute()
public:
AssemblyInitializeAttribute()
new : unit -> AssemblyInitializeAttribute
public function AssemblyInitializeAttribute()

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 AssemblyInitialize attribute on the AssemblyInit()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

.NET Framework Security

See Also

Reference

AssemblyInitializeAttribute Class

Microsoft.VisualStudio.TestTools.UnitTesting Namespace