Testinitialize VS Classinitialize

Using MS Test ClassInitialize() and TestInitialize() in VS2010 as opposed to NUnit

Here is a simple example using TestInitialize and TestCleanup.

[TestClass]
public class UnitTest1
{
private NorthwindEntities context;

[TestInitialize]
public void TestInitialize()
{
this.context = new NorthwindEntities();
}

[TestMethod]
public void TestMethod1()
{
Assert.AreEqual(92, this.context.Customers.Count());
}

[TestCleanup]
public void TestCleanup()
{
this.context.Dispose();
}
}

What's the difference between using the constructor in VS Testing framework vs. TestInitialize() attribute?

This post gives an overview of the different methods. As you can see, the ctor is called immediately before the ClassInitialize (only once, of course) and TestInitialize.

So put stuff that requires code in ClassInitialize in your TestInitialize method. Everything that should be set up before ClassInitialize goes in the ctor.

Obviously, TestInitialize content will be executed once before each test. The corresponding method to close after each test is TestCleanup. For classes, use ClassCleanup. The same thing exists for assemblies as well (AssemblyInitialize/Cleanup).

Further reading

Why does TestInitialize get fired for every test in my Visual Studio unit tests?

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() { }

Initialise unit test class members upon declaration or in the TestInitialize method?

No, there is nothing wrong with initializing members in the declaration.

Primarily it comes down to preferred style. I prefer to make as many members readonly as I can, so I prefer initializing in the declaration when possible.

Others may prefer use of setup/teardown methods for all initialization and cleanup logic.

Do you use TestInitialize or the test class constructor to prepare each test? and why?

The constructor is just a structure provided by the language. Every test framework seems has its own controlled lifecycle "initialize". You'll probably only get into trouble using the constructor to mutate your locals.

MSTest: You get an entire new instance of the test class for every TestMethod. This might be the only case where it's ok to mutate your locals in the constructor, initializer, or test method and not affect the other test methods.

public class TestsForWhatever
{
public TestsForWhatever()
{
// You get one of these per test method, yay!
}

[TestInitialize]
public void Initialize()
{
// and one of these too!
}

[TestMethod]
public void AssertItDoesSomething() { }

[TestMethod]
public void AssertItDoesSomethingElse() { }
}

MSpec: You only get one Establish and Because for all your assertions (It). So, don't mutate your locals in your assertions. And don't depend on mutations of locals in base contexts (if you use them).

[Subject(typeof(Whatever))]
public class When_doing_whatever
{
Establish context = () =>
{
// one of these for all your Its
};

Because of = () => _subject.DoWhatever();

It should_do_something;
It should_do_something_else;
}


Related Topics



Leave a reply



Submit