I'm using Visual Studio 2010 Beta 2. I've got a single [TestClass]
, which has a [TestInitialize]
, [TestCleanup]
and a few [TestMethods]
.
Every time a test method is run, the initialize and cleanup methods are ALSO run!
I was under the impression that the [TestInitialize]
& [TestCleanup]
should only be run once, per local test run.
Is that correct? If not, what is the proper way to do this?
TestInitialize
and TestCleanup
are ran before and after every test in the same class they are defined, this to ensure that no test is coupled to any other test in the same one class and to always start each test in a clean state.
If you want to run methods before and after all tests in a particular class, decorate relevant methods with the ClassInitialize
and ClassCleanup
attributes.
If you want to run methods before and after all tests in a particular test project (assembly), decorate relevant methods with the AssemblyInitialize
and AssemblyCleanup
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() { }