views:

596

answers:

2

Hi folks,

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 ran, the initialize and cleaup methods are ALSO ran!

I was under the impression that the [TestInitialize] & [TestCleanup] should only be ran once, per local test run.

Is that correct? If not, what is the proper way to do this?

A: 

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.

stijn
+5  A: 

TestInitialize and TestCleanup is 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() { }
alexn
Cheers :) the 2x Class ones are what i need :) awesome.
Pure.Krome