TestInitialize and TestCleanup are ran before and after each test, this is to ensure that no tests are coupled. If you want to run methods before and after ALL tests, decorate relevant methods with the ClassInitialize and ClassCleanup attributes.
To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).
TestInitialize. This attribute is needed when we want to run a function before execution of a test. For example we want to run the same test 5 times and want to set some property value before running each time. In this scenario we can define one function and decorate the function with a TestInitialize attribute.
TestInitialize
and TestCleanup
are ran before and after each test, this is to ensure that no tests are coupled.
If you want to run methods before and after ALL tests, decorate relevant methods with the ClassInitialize
and ClassCleanup
attributes.
Relevant information from the auto generated test-file in Visual Studio:
You can use the following additional attributes as you write your tests:
// Use ClassInitialize to run code before running the first test in the class
[ClassInitialize()]
public static void MyClassInitialize(TestContext testContext) { }
// Use ClassCleanup to run code after all tests in a class have run
[ClassCleanup()]
public static void MyClassCleanup() { }
// Use TestInitialize to run code before running each test
[TestInitialize()]
public void MyTestInitialize() { }
// Use TestCleanup to run code after each test has run
[TestCleanup()]
public void MyTestCleanup() { }
this is rather standard behaviour for test suites: setup and teardown before and after each test. The philosophy is that tests should not depend on each other. If you want another behaviour, you should probably use static objects that persist between each test.
Full example taken from the microsoft documentation:
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With