ClassInitializeAttribute Class
Assembly: Microsoft.VisualStudio.QualityTools.UnitTestFramework (in microsoft.visualstudio.qualitytools.unittestframework.dll)
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 ClassInitializeAttribute.
-
Methods marked with the TestInitializeAttribute.
-
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.
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); } } }
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); } } }
System.Attribute
Microsoft.VisualStudio.TestTools.UnitTesting.ClassInitializeAttribute
The constructor is obviously not static and runs with each initialization of the test class (which is one per test method)
For more information see:
http://blogs.msdn.com/b/nnaderi/archive/2007/02/17/explaining-execution-order.aspx
- 9/23/2010
- yosi142
[tfl - 03 08 09] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at
http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
Visual Studio : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
SQL Server : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.sqlserver%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
PowerShell : http://groups.google.com/group/microsoft.public.windows.powershell/topics?pli=1
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&
- 10/30/2008
- Derek Morrison
- 8/3/2009
- Thomas Lee
Important